Laboratory 7 
Riemann Sums

Mathematica Code

This lab assignment relies on the module displayRiemannSums to explore the concept of a Riemann sum. The module’s definition appears at the end of this file. To use that module, simply select “Yes” when Mathematica prompts you to “evaluate initialization cells.” The syntax of displayRiemannSums is very simple: the command

displayRiemannSums[100]

should result in a dynamic plot of the function y=(e^(-(x^2))) on the interval [-1,2] and the value of a single-rectangle left Riemann sum for this function. There are several controls that you can use to change the output:
1. You may type a different formula in place of (e^(-(x^2))) in the field labeled “f(x)”.
2. You may use the fields labeled “a” and “b” to change the interval [-1,2] to any other interval [a,b].
3. You may use the slider labeled “n” to set the number of subintervals in the Riemann sum to any integer between 1 and 100. The maximum value of n is set by the input parameter in the call to displayRiemannSums. (So, if you want to see Riemann sums with n=20,30  and  40, you need to replace the input argument 10 above by an integer ≥40.)
4. You may use the menu labeled “type” to select the type of Riemann sums you want to study—that is, you get to choose how Mathematica will select the sample points for the Riemann sums. Available options are: left endpoints, right endpoints, midpoints, and random points. The first three options match the types of Riemann sums that you will find in the textbook and in the lectures: left and right Riemann sums that choose consistently left and right endpoints as the sample points, and Riemann sums using the so-called “midpoint rule”. When you select the “random points” option, Mathematica selects the sample point in each subinterval at random.

Experiment

Experiment 1

For each of the functions f and the intervals [a,b] given below, follow instructions (1) through (4):
	(a) f(x)=3(x^2)+2, on [0,2].
	(b) f(x)=Cos[x]-Sin[x], on [0,(π/2)]. 
	(c) f(x)=x-(e^x), on [0,1].

(1) Use the module displayRiemannSums to generate plots of left and right Riemann sums that approximate the area under the graph y=f(x) using 10 rectangles. Copy and paste the plots from the dynamic output in the empty space below. Then state whether the left and right Riemann sums consistently overestimate/underestimate the area under the curve.



(2) Use the module displayRiemannSums to fill the following table with the values of the required Riemann sums.
------------------------------------------------------------------------------------------
n	Left endpoint sum	Right endpoint sum	Midpoint sum	
------------------------------------------------------------------------------------------
10
25
50
100
------------------------------------------------------------------------------------------

(3) Use the module displayRiemannSums to generate 4 different randomized Riemann sums based on 25 rectangles each and record the values in the table below. Then compute the average of the 10 randomizes sums and compare its value to your estimate for the area from part (3).   
--------------------------------------------------
Trial	Randomized sum with n=25	
--------------------------------------------------
1
2
3
4
--------------------------------------------------

(4) Look at the table you produced in part (2) for f(x) on the given interval (particularly the midpoint sum). Record the limit that the Riemann sums appear to be approaching.



(5) Find an antiderivative for f(x) and record the values of that antiderivative at each endpoint, a and b.


(6) Say how the limit you recorded in part (1) relates to the two values of the antiderivative in part (2).

displayRiemannSums[n_]:=Manipulate[
Module[{func},
Clear[x,formula];
Clear[e];
e=Exp[1];
func[x_,formula_]=Function[x,formula];
Module[{f=func[x,expr],h,j,ξ,η,p1,p2},
h=(b-a)/m;
p1=Plot[f[x],{x,a,b},PlotStyle->Thick,PlotRange->All,Frame->True,Axes->False,ImageSize->Medium];
ξ=Switch[type,
"left endpoints",Table[1,{m}],
"right endpoints",Table[0,{m}],
"midpoints",Table[1/2,{m}],
"random points",Table[RandomInteger[10]/10,{m}]];
η=Table[a+j*h,{j,1,m}];
p2=Graphics[{
{LightBlue,
Table[Polygon[{{η[[j]]-h,0},{η[[j]],0},{η[[j]],f[η[[j]]-ξ[[j]]*h]},{η[[j]]-h,f[η[[j]]-ξ[[j]]*h]}}],
{j,1,m}]},
Table[
Line[{{η[[j]]-h,0},{η[[j]]-h,f[η[[j]]-ξ[[j]]*h]},{η[[j]],f[η[[j]]-ξ[[j]]*h]},{η[[j]],0},{η[[j]]-h,0}}],
{j,1,m}],
{PointSize->0.015,Blue,
Table[Point[{η[[j]]-ξ[[j]]*h,0}],{j,1,m}]}}];
Column[{
Show[p1,p2,p1],
"Riemann sum:",
N[ Sum[f[η[[j]]-ξ[[j]]*h]*h, {j,1,m} ]]}]
]],
{type,{"left endpoints","right endpoints", "midpoints", "random points"},ControlType->PopupMenu},{{expr,Exp[-(x^2)],"f(x)="},ControlType->InputField},
Row[{
Control[{{a,-1,"a="},ControlType->InputField,FieldSize->10}],"      ",
Control[{{b,2,"b="},ControlType->InputField,FieldSize->10}]}],
{{m,1,"n"},1,n,1,Appearance->"Labeled"}]

displayRiemannSums[100]

Experiment 1 results