# Riemann Sums: A Symbolic and Graphical Approach

This Mathematica notebook allows the investigation of left, right, and midpoint Riemann sums to approximate the definite integral of y = f(x) on [a,b] as well as sums obtained from the trapezoid and Simpson's rules.

The sums are expressed in terms of Δx and the limits are investigated as Δx approaches 0 (from the left and right). In addition, rates of convergence to the limit are examined as well as improvements to the approximations that give faster convergence.

IMPORTANT:  If you use different functions, you will need to make modifications in the PlotRange options in the functions that generate the plots.

This Mathematica notebook generates the graphs, animations, and data in the example in the demo entitled Riemann Sums: A Symbolic and Graphical Approach.

Define the limits of integration.

Define f.

Save the plot of f.  For a nice display, use a plotting interval slightly larger than [a,b].

Maximum number of (equal width) rectangles is maxnumpts.  Δx (depends on n) and is equal to (b-a)/n.

Define the function to generate rectangles for Right Hand Endpoint approximation.

Define the Right Hand Endpoint (RHE) Riemann sum as a function of n and Δx.

Define the function to show the approximations to the definite integral using approximation scheme approx.

Show the animation of graphics array of rectangles and associated approximation to definite integral.

Define function to generate rectangles for Left Hand Endpoint approximation.

Define the Left Hand Endpoint (LHE) Riemann sum as a function of n and Δx.

Show the animation of graphics array of rectangles and associated approximation to definite integral.

Define the function to generate rectangles for Right Hand Endpoint approximation.

Define the Left Hand Endpoint (LHE) Riemann sum as a function of n and Δx.

Show the animation of graphics array of rectangles and associated approximation to definite integral.

Show the animation of approximations, simultaneously. Right hand endpoint in red, left hand endpoint in blue and midpoint approximation in green.

Show the animation of approximations simultaneously, but as functions of Δx.

Investigate the limits and symbolic representations of the approximations (as functions of Δx).

Define the trapezoidal and Simpson Rule approximations.

Evaluate limit.  The value lim is required for plots (to investigate convergence rates) below.

The next sequence of commands sets up animations of the approximations obtained by halving the step size.  The purpose of this is to look at the convergence rate of the approximation scheme.  Upon halving the step size, the ratio of the errors generated by consecutive step sizes should approach 1/2 for approximation schemes that converge linearly(right and left hand approximations), 1/4 for approximation schemes that converge quadratically (midpoint and trapezoidal approximations), and 1/16 for Simpson's Rule.

In addition, the commands define computations to generate tabular data from the approximations.

Error at each step.

Ratio of errors at consecutive steps.

If the error is smaller than 0.00001, it will be displayed as 0.

Sets up text strings for graphical display.

Defines the animation that will display 1) approximations as a function of Δx, 2) horizontal line y = lim (where lim is the limiting value), 3) a vertical line whose length is the (absolute) error between the approximation and the actual limit.

Show the animation of the Right Hand Endpoint approximation.

Display tabular data from the computation.

 n Δx Approximate Error Approximate Ratio 1 2.` 121.46666666666667` 0.4154774972557629` 2 1.` 50.46666666666667` 0.4439398943196829` 4 0.5` 22.404166666666665` 0.46769806583596807` 8 0.25` 10.478385416666667` 0.4826356960011929` 16 0.125` 5.057242838541667` 0.4909928085685688` 32 0.0625` 2.4830698649088543` 0.49541217338117793` 64 0.03125` 1.2301430384318033` 0.49768464011405145` 128 0.015625` 0.6122232953707377` 0.4988369086689538` 256 0.0078125` 0.3053995760778586` 0.4994170951968811` 512 0.00390625` 0.15252176915916305` 0.4997082070231353` 1024 0.001953125` 0.0762163797985219` 0.49985401826859016` 2048 0.0009765625` 0.038097063700176174` 0.4999269878111387` 4096 0.00048828125` 0.01904575030007815` 0.4999634885732277` 8192 0.000244140625` 0.00952217976252167` 0.4999817429533342` 16384 0.0001220703125` 0.00476091603438055` 0.499990871143323` 32768 0.00006103515625` 0.002380414555470146` 0.4999954354883224` 65536 0.000030517578125` 0.001190196412305037` 0.499997717723326` 131072 0.0000152587890625` 0.0005950954897950093` 0.49999885885645423` 262144 7.62939453125`*^-6 0.00029754706580812734` 0.49999942942692477` 524288 3.814697265625`*^-6 0.00014877336313171932` 0.4999997147131369`

Show the animation of the Left Hand Endpoint approximation.

Display tabular data from the computation.

 n Δx Approximate Error Approximate Ratio 1 2.` 34.53333333333333` 0.7972972972972974` 2 1.` 27.533333333333335` 0.6027542372881356` 4 0.5` 16.595833333333335` 0.5436072056239015` 8 0.25` 9.021614583333333` 0.5201682158011719` 16 0.125` 4.692757161458333` 0.5097067785088252` 32 0.0625` 2.3919301350911457` 0.5047626366069382` 64 0.03125` 1.2073569615681967` 0.5023590569614677` 128 0.015625` 0.6065267046292623` 0.5011740152611178` 256 0.0078125` 0.3039754239221414` 0.5005856357644619` 512 0.00390625` 0.15216573084083695` 0.5002924757158769` 1024 0.001953125` 0.07612737020147808` 0.5001461524160803` 2048 0.0009765625` 0.038074811299823826` 0.5000730548600237` 4096 0.00048828125` 0.01904018719992185` 0.5000365220945626` 8192 0.000244140625` 0.00952078898747833` 0.5000182597136132` 16384 0.0001220703125` 0.004760568340619449` 0.500009129523414` 32768 0.00006103515625` 0.002380327632029854` 0.5000045646783619` 65536 0.000030517578125` 0.001190174681444963` 0.500002282318345` 131072 0.0000152587890625` 0.0005950900570799907` 0.5000011411539635` 262144 7.62939453125`*^-6 0.00029754570762937266` 0.5000005705756796` 524288 3.814697265625`*^-6 0.00014877302358703068` 0.5000002852875142`

Show the animation of the Midpoint approximation.

Display tabular data from the computation.

 n Δx Approximate Error Approximate Ratio 1 2.` 20.533333333333335` 0.27556818181818177` 2 1.` 5.658333333333333` 0.255798969072165` 4 0.5` 1.4473958333333334` 0.2514168765743073` 8 0.25` 0.3638997395833333` 0.2503522229179712` 16 0.125` 0.09110310872395834` 0.25008793184305145` 32 0.0625` 0.022783788045247395` 0.25002197523147246` 64 0.03125` 0.005696447690327963` 0.2500054933249998` 128 0.015625` 0.0014241432150204976` 0.25000137330107397` 256 0.0078125` 0.0003560377595325311` 0.25000034332338256` 512 0.00390625` 0.00008900956211922069` 0.25000008583072775`

Show the animation of the Trapezoidal approximation.

Display tabular data from the computation.

 n Δx Approximate Error Approximate Ratio 1 2.` 43.46666666666667` 0.26380368098159507` 2 1.` 11.466666666666667` 0.25327034883720934` 4 0.5` 2.904166666666667` 0.2508070301291248` 8 0.25` 0.7283854166666667` 0.25020110833035397` 16 0.125` 0.18224283854166667` 0.2500502366705368` 32 0.0625` 0.04556986490885417` 0.2500125566444182` 64 0.03125` 0.011393038431803386` 0.2500031390034431` 128 0.015625` 0.0028482953707377116` 0.25000078474100756` 256 0.0078125` 0.000712076077858607` 0.25000019618463604` 512 0.00390625` 0.00017801915916303794` 0.25000004904612055`

Show the animation of the Simpson's Rule approximation.

 n Δx Approximate Error Approximate Ratio 1 2.` 0.8` 0.0625` 2 1.` 0.05` 0.0625` 4 0.5` 0.003125` 0.0625` 8 0.25` 0.0001953125` 0.0625` 16 0.125` 0.00001220703125` 0.0625`

This Mathematica notebook was based on the ideas discussed in
"Symbolic and Graphical Investigations of Riemann Sums with a Computer Algebra System", PRIMUS, VI,No.4 (December 1996), 366-380 by Dr. Lawrence H. Riddle from Agnes Scott College.

Mathematica notebook by

Lila F. Roberts
Mathematics and Computer Science Dept.
Georgia College & State University
Milledgeville, GA  31061
lila.roberts@gcsu.edu

Converted by Mathematica      June 7, 2001