Wichita State University Logo

Math 451: Computational Mathematics

3.5 Integration


3.5.1 Riemann Sums

Methods in numerical integration arise from the definition of the Riemann Integral

$$ \displaystyle\int_a^b\,f(t)\,dt = \displaystyle\lim_{n\rightarrow\infty} \displaystyle\sum_{k=1}^n f(x_k^*)\Delta x $$
The Riemann Sum

$$ \displaystyle\sum_{k=1}^n f(x_k^*)\Delta x $$
is an approximation of the area under the curve obtained by

  1. partitioning the interval $[a,b]$ into $n$ non-overlapping subintervals $[x_k, x_{k+1}]$

    $$ a = x_0 < x_1 < x_2 < \dots < x_{n-1} < x_n = b $$
    whose union is all of $[a,b]$.

  2. choosing a value $x_k^*\in[x_k, x_{k+1}]$ for $1\le k \le n$.

  3. evaluating the integrand $f(x)$ at each chosen point to obtain a height of $n$ rectangles.

  4. computing the areas of each rectangle $f(x_k^*)\Delta x$, where $\Delta x = \left(\frac{b-a}{n}\right)$

  5. summing the areas of the rectangles to obtain the approximation.

In [14]:
riemannsum(@(x) cos(x), 0, pi/4, 10, 'l', 'b', 'FaceAlpha', .7)
ans =

      0.71825


The riemannsum function used for this notebook can choose the value in each interval of the partition

$$ x_k^*\in[x_k, x_{k+1}] $$
utilizing one of several methods.

  • 'l' left end point of each subinterval (Left Sum)
  • 'r' right end point of each subinterval (Right Sum)
  • 'm' midpoint of each subinterval (Middle Sum)
  • 's' a uniformly distributed random value in each subinterval (True Riemann Sum)
  • 't' use both end points to create a trapezoid instead of a rectangle
In [17]:
method = [ 'l', 'r', 'm', 's', 't' ];
 name  =  { 'left sum', 'right sum', 'middle sum', 'Riemann sum', 'trapezoid rule'};
 for k=1:5
     subplot(2,3,k)
     riemannsum(@(x) cos(x), 0, pi/4, 10, method(k), 'b');
     title(name{k})
 end

       

3.5.2 Accuracy

Computing the cost or accuracy of these methods of computing Riemann sums depends on the shape of the integrand function. However we can compare the convergence of each method for the same function.

In [61]:
fprintf('%14s %18s %19s %18s %19s\n', ...
'Left Sum', 'Right Sum', 'Middle Sum', 'Riemann Sum', 'Trapezoid Rule')
fprintf('%s\n',repmat('-',1,94))
f = @(x) cos(x);
results = zeros(50,5);
for k=1:4:201
    results(k,:) = [ riemannsum(f, 0, pi/2, k, 'l'), ...
                     riemannsum(f, 0, pi/2, k, 'r'), ...
                     riemannsum(f, 0, pi/2, k, 'm'), ...
                     riemannsum(f, 0, pi/2, k, 's'), ...
                     riemannsum(f, 0, pi/2, k, 't') ];
                
    fprintf('%18.16f %18.16f %18.16f %18.16f %18.16f\n', results(k,:))
end
      Left Sum          Right Sum          Middle Sum        Riemann Sum      Trapezoid Rule
----------------------------------------------------------------------------------------------
1.5707963267948966 0.0000000000000001 1.1107207345395915 0.0619292112950529 0.7853981633974483
1.1488414014342170 0.8346821360752377 1.0041242039539870 0.9776705959682687 0.9917617687547273
1.0847266943914424 0.9101937691920094 1.0012703678331201 0.9958979637541698 0.9974602317917260
1.0591982799702861 0.9383677932937554 1.0006085927531818 1.0101974058761747 0.9987830366320209
1.0454883140316482 0.9530885301025366 1.0003558269410320 1.0117862535724560 0.9992884220670925
1.0369336179061805 0.9621337928207093 1.0002331636252535 0.9931091156332663 0.9995337053634452
1.0310869180740290 0.9682550650022329 1.0001645123493130 1.0017585615255911 0.9996709915381310
1.0268381925388665 0.9726728019597322 1.0001222558589231 1.0017823158029928 0.9997554972492992
1.0236111247646831 0.9760112360739287 1.0000944124642879 0.9991624304598704 0.9998111804193062
1.0210767780208680 0.9786228232426277 1.0000751013761036 1.0052403254461413 0.9998498006317479
1.0190337316541538 0.9807216261225709 1.0000611616779971 0.9990110190291195 0.9998776788883623
1.0173517513179153 0.9824451662780287 1.0000507713743070 1.0003208629876501 0.9998984587979720
1.0159428945132496 0.9838858266194762 1.0000428202668750 0.9995194950985602 0.9999143605663630
1.0147456329268227 0.9851079663835229 1.0000366005742840 1.0010738882344787 0.9999267996551733
1.0137156281787361 0.9861577978840885 1.0000316437846863 1.0009634834097922 0.9999367130314120
1.0128201206494056 0.9870693611937514 1.0000276297682273 1.0007102228896156 0.9999447409215787
1.0120343815055888 0.9878682841702826 1.0000243337586687 0.9996450064307529 0.9999513328379357
1.0113393939802611 0.9885742298238134 1.0000215941888722 1.0010016231371412 0.9999568119020370
1.0107202941608020 0.9892025362595021 1.0000192925065823 0.9994475400822734 0.9999614152101526
1.0101652960696710 0.9897653437736335 1.0000173401293762 0.9997827779868509 0.9999653199216525
1.0096649341850403 0.9902723869406589 1.0000156697922373 1.0006049060091564 0.9999686605628497
1.0092115191640256 0.9907315623782033 1.0000142296751875 1.0009388332330822 0.9999715407711145
1.0087987398960372 0.9911493429657575 1.0000129793350896 0.9992193796283300 0.9999740414308974
1.0084213679569838 0.9915310848731678 1.0000118868348506 1.0006140142987996 0.9999762264150760
1.0080750349869816 0.9918812584220859 1.0000109266835508 0.9996108582048658 0.9999781467045341
1.0077560628287305 0.9922036239495730 1.0000100783358961 0.9992045821588608 0.9999798433891514
1.0074613323928732 0.9925013673757788 1.0000093250839244 0.9991281003099879 0.9999813498843255
1.0071881813265098 0.9927772058513270 1.0000086532280039 0.9994648907207365 0.9999826935889189
1.0069343233646610 0.9930334709151486 1.0000080514494958 1.0004327687654999 0.9999838971399044
1.0066977841874094 0.9932721745566840 1.0000075103308979 0.9999271976894180 0.9999849793720466
1.0064768499716685 0.9934950621469174 1.0000070219851460 1.0005142984005282 0.9999859560592930
1.0062700258000101 0.9937036551856510 1.0000065797665725 0.9999992332643475 0.9999868404928308
1.0060760017914667 0.9938992860798784 1.0000061780436147 1.0006979706171981 0.9999876439356721
1.0058936253318727 0.9940831266341666 1.0000058120186237 1.0006068166054956 0.9999883759830190
1.0057218781595496 0.9942562115406086 1.0000054775839615 0.9999978822624144 0.9999890448500790
1.0055598573439652 0.9944194578631503 1.0000051712062432 1.0006129335193401 0.9999896576035576
1.0054067594069960 0.9945736812911692 1.0000048898326312 1.0002231792785636 0.9999902203490831
1.0052618669972977 0.9947196097704863 1.0000046308144874 0.9998827731897048 0.9999907383838916
1.0051245376513414 0.9948578949925512 1.0000043918448138 1.0004074779268384 0.9999912163219458
1.0049941942696248 0.9949891221244345 1.0000041709067036 0.9998947588134522 0.9999916581970303
1.0048703170102404 0.9951138180860484 1.0000039662306470 0.9998745459532448 0.9999920675481438
1.0047524363596563 0.9952324586215053 1.0000037762589877 0.9998713064761127 0.9999924474905806
1.0046401271860139 0.9953454743647423 1.0000035996161982 1.0000424501837677 0.9999928007753786
1.0045330036161209 0.9954532560623930 1.0000034350839115 0.9998232590077226 0.9999931298392570
1.0044307146060543 0.9955561590874391 1.0000032815798572 1.0001918812103696 0.9999934368467472
1.0043329400982421 0.9956545073535189 1.0000031381400150 1.0001629251169128 0.9999937237258797
1.0042393876763502 0.9957485967207023 1.0000030039034433 1.0002452868932967 0.9999939921985268
1.0041497896443847 0.9958386979682213 1.0000028780993349 1.0001073900819306 0.9999942438063024
1.0040639004684779 0.9959250593970018 1.0000027600359152 0.9998327479506095 0.9999944799327404
1.0039814945299554 0.9960079091147529 1.0000026490909277 0.9999163800787866 0.9999947018223547
1.0039023641463138 0.9960874570478320 1.0000025447034067 0.9999545396780375 0.9999949105970731

As we can see, the left sum and right sum do not converge to the limit as quickly as the midpoint sum and trapezoid rule converge.

3.5.3 Adaptive Riemann Sum

Approximating the area with rectangles and trapezoids becomes less accurate with functions that vary their curvature a great deal over our interval of integration $[a,b]$.

In [65]:
f = @(x) sin(x)./x;
riemannsum(f, pi/64, 3*pi, 1000, 't', 'b')
ans =

       1.6257


The adaptive version of riemannsum

  1. first computes $I_1$ the Riemann sum using only one subinterval $[a,b]$

  2. this it recursively completes the following for interval $[a,b]$ passing estimate $I_1$

  3. next computes the Riemann sum for both $\left[a, \frac{a+b}{2}\right]$, $I_2$ and $\left[\frac{a+b}{2}, b\right]$, $I_3$.

  4. the algorithm compares the values of $|I_1 - (I_2 + I_3)| < $ tolerance.

  5. if the values are with the specified tolerance of each other the area approximation is $I_2 + I_3$, otherwise it invokes step 3 twice

    • once for interval $\left[a, \frac{a+b}{2}\right]$ passing estimate $I_2$, and
    • once for interval $\left[\frac{a+b}{2}, b\right]$ passing estimate $I_3$.
  6. recursion continues until a maximum recursion depth is achieved and a warning issued, or the desired estimate is computed.

Adaptive algorithms can be accurate, and efficient. Adaptive methods will accept estimates on wide subintervals over which the function is featureless and continuous. It will only require smaller subintervals where the function is steep or irregular. However they should be used cautiously.

Adaptive methods can be very inefficient if the integrand has a discontinuity in the interval of integration because unnecessary subdivision will occur. One should start out by breaking up such regions into subintervals so that discontinuities occur between subintervals.

Exercise 3.5.1

Re-write riemannsum.m so that the function riemannsum has arguments

function area = riemannsum(f, a, b, n, method, tolerance, c, varargin)
  riemannsum computes and graphs the Riemann sum of an integrable function 
    f, on an interval [a,b] using n equal rectangular polygons.

  Inputs:
    f           a handle to an integrable function
    a           left end point of interval of integration 
    b           right end point of interval of integration
    n           the maximum depth of recursion
    method      the method for computing the Riemann sum is a single character
                string chosen from 'l','r','m','s','t' for 
                'left sum', 'right sum', 'middle sum', 'random', 'trapezoid'
                option 's' chooses a pseudo-random value in each interval of
                the partition of [a,b]. 
    tolerance   the required acceptable error in the computation of the area
    c           color must be a single character string chosen from the list 
                'r','g','b','c','m','y','w','k', or 
                an RGB row vector triple, [r g b], the rectangles are 
                filled with the constant specified color.
    varargin    the variable argument list modifies one or more Name,Value
                argument pairs to set the patch properties of the filled
                rectangles of the graph. For example
                FaceColor - this is the fill color you set with C
                FaceAlpha - alpha blending the faces for transparency [0,1]
                EdgeColor - an RGB row vector triple line color specification 
                LineStyle - for plotting edges
  Outputs:
    area        the area between the curve and the x-axis

You can find the source to riemannsum here . You are adding an new argument tolerance to use to stop the algorithm instead of the number of intervals. You are changing the meaning the input argument n from the number of subintervals to the maximum recursion depth .

You will need to make some simple modifications to the main function riemannsum.m along with the addition of the tolerance argument.

  1. Make sure you set default values for the arguments. You are changing the ordering of the arguments, so pay attention to that. Your default values for the tolerance should be 1e-20 .
  2. Presently, riemannsum calls GraphRiemannSum in the main body to set up the computation, you want to change this to instead call your own function that controls the recursion. For you assignment, you are permitted to ignore the graphing features and completing the assignment should be possible without you interacting with GraphRiemannSum at all.
  3. You will write a brand new subfunction that does the adaptive recursion. This function will use ComputeRiemannSum to find the area, passing it the correct arguments to return area values. The recursion process will be controlled entirely by this function, which will call both ComputeRiemannSum and itself (name it what you would like) recursively. It should follow the algorithm described above.

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.