Laboratory 14
Riemann Sums and Definite Integrals

Introduction

In this lab, we introduce the definite integral which is closely related to finding the area of certain regions in the coordinate plane. In particular, we are interested in regions bounded by graphs of functions. To find the area of such regions, we will utilize a limiting process of a certain sum, called the Riemann sum.

14.1 Left Riemann Sums

Given a continuous function f(x), a≤x≤b, we are interested in the area of the plane region R bounded by the graph of y=f(x), the x-axis, and the vertical lines x=a and x=b. We refer to R as the “region under the graph of y=f(x).” For a concrete example, consider the function f(x)=(e^(-(x^2))), whose graph in the interval [a, b] is plotted below. We will assume that a=0 and b=2.

Clear[f,a,b];f[x_]:=E^(-x^2);a=0;b=2;

Plot[f[x],{x,a,b},Filling->Bottom,AxesLabel->{"x",""},
		                PlotStyle->{{RGBColor[1,0,.1],Thickness[.008]}}]

Our aim is to approximate the area of the shaded region R shown above. We now show one way to do that. 

We first partition the interval [a,b] into n subintervals, each of width (b-a)/n. We then generate n rectangles. Each rectangle has one of the subintervals as its base. Furthermore, the height of each rectangle is the value of the function f at the left endpoint of the base of that rectangle. Put together, all the rectangles form a polygon whose area approximates the area of the region R above.

To illustrate the above idea, we now define a custom command, plotLeftSum, which displays the graph y=f(x) in the interval [a,b] and the resulting n rectangles. This command is executed as follows:

	plotLeftSum[f,a,b,n]

where the four inputs are: 
	f	the name of the function, 
	a,b	the endpoints of the interval, 
	n	the number of subintervals.

plotLeftSum[f_,a_,b_,n_]:=Module[{h,p1,p2},
h:=(b-a)/n;
p1=Plot[f[x],{x,a,b},PlotStyle->Thick,PlotRange->All,Frame->True,Axes->False,ImageSize->Medium];
p2=Graphics[{
{LightBlue,Table[Polygon[{{x,0},{x+h,0},{x+h,f[x]},{x,f[x]}}],{x,a,b-h,h}]},Table[Line[{{x,0},{x,f[x]},{x+h,f[x]},{x+h,0}}],{x,a,b-h,h}]}];
Show[p1,p2,p1]
]

For example, here are the four rectangles resulting from the application of the above procedure to the function f(x)=(e^(-(x^2))) on [0,2]. We will refer to this graph as Figure 1.

plotLeftSum[f,0,2,4]

The above command can be executed with a different value for the number of subintervals n to get the corresponding graph. For example, the graph that corresponds to n=8 can be generated as follows.

plotLeftSum[f,0,2,8]

Suppose that we want to compute the total area of the n rectangles constructed to approximate the region R. We could find the area of each rectangle by multiplying the height times the base; then we add the results. For example, for n=4 the area is calculated as follows.

f[0]*.5 + f[.5]*.5 + f[1]*.5 + f[1.5]*.5

This calculation is tedious, especially if n is large. To simplify this task, we now define a function, leftSum, whose value is the total area of the n rectangles. This command has five parameters:
	f	the name of the function, 
	a,b	the endpoints of the interval, 
	n	the number of subintervals,
	k	desired precision.	
You may recognize the sum that appears on the right side of this definition as what we called a Riemann sum  in the lectures.

leftSum[f_,a_,b_,n_,k_]:=N[ Sum[f[a+i*(b-a)/n]*(b-a)/n, {i,0,n-1} ],k]

The following command gives the area of the 4 rectangles shown above.

leftSum[f,0,2,4,8]

This matches, as expected, what was obtained before. However, this latter method can be used for any value of n. So, for the case n=8, we get

leftSum[f,0,2,8,8]

We can also try to compare different values of the Riemann sums with increasing values for n. For example, the following command allows us to see how those values vary as n varies from 1 to some assigned maximum value. The input arguments are the same as for  leftSum, but it displays both the results of plotLeftSum and leftSum.

showLeftSums[f_,a_,b_,n_,k_]:=Module[{g,i},
g[x_]=f[x];
Manipulate[
Column[{
plotLeftSum[g,a,b,i],
"Left Riemann sum:",
leftSum[g,a,b,i,k]}],
{{i,1,"n"},1,n,1}]]

showLeftSums[f,0,2,40,10]

Experiment 14.1

For each of the functions f and the intervals [a, b] given below, do the following:
	(a) Graph the region R under the curve y=f(x) between x=a and x=b. 
	(b) Use the module showLeftSums to show a graphical illustrations for approximations of the area of the region R under the graph of f using 10 rectangles.
	(c) Tabulate the values of the left Riemann sums for n=4, 8, ..., 40.
	(d) Estimate the area of the region R.
	
	1. f(x)=(Sqrt[(x^3)+0.1]), on [0,2].
	
	2. f(x)=ln(5 cos x), on [0, 1.2].

14.2 Right Riemann Sums

The rectangles used above are such that the height of the rectangle on the subinterval [(Subscript[x, i-1]), (Subscript[x, i]) ] is f((Subscript[x, i-1])).  We now modify plotLeftSum so that the height of the rectangle on the subinterval [ (Subscript[x, i-1]), (Subscript[x, i])] is f((Subscript[x, i])) instead of f((Subscript[x, i-1])). The new function will be called plotRightSum. We will use it to generate the graph corresponding to the one in Section 14.1 for n=4. We will refer to the graph we obtain as Figure 2.

plotRightSum[f_,a_,b_,n_]:=Module[{h,p1,p2},
h:=(b-a)/n;
p1=Plot[f[x],{x,a,b},PlotStyle->Thick,PlotRange->All,Frame->True,Axes->False,ImageSize->Medium];
p2=Graphics[{
{LightBlue,Table[Polygon[{{x,0},{x+h,0},{x+h,f[x+h]},{x,f[x+h]}}],{x,a,b-h,h}]},Table[Line[{{x,0},{x,f[x+h]},{x+h,f[x+h]},{x+h,0}}],{x,a,b-h,h}]}];
Show[p1,p2,p1]
]

Clear[f]
f[x_]:=E^(-x^2)
plotRightSum[f,0,2,4]

Notice that, on this plot, the shaded area under-estimates that of region R. We now modify the function leftSum to get rightSum, which gives the area of these rectangles.

rightSum[f_,a_,b_,n_,k_]:=N[ Sum[f[a+i*(b-a)/n]*(b-a)/n, {i,1,n} ],k]

Evaluating rightSum for n=4, we get

rightSum[f,0,2,4,8]

We also define a version of showLeftSums, named showRightSums, which we can use to compare the values of the right Riemann sums.

showRightSums[f_,a_,b_,n_,k_]:=Module[{g,i},
g[x_]=f[x];
Manipulate[
Column[{
plotRightSum[g,a,b,i],
"Right Riemann sum:",
rightSum[g,a,b,i,k]}],
{{i,1,"n"},1,n,1}]]

showRightSums[f,0,2,40,10]

It should be evident from experimentation with the above plots that the left Riemann sums of the function f(x)=(e^(-(x^2))), 0≤x≤2, always overestimate the area of the region R and that the right Riemann sums always underestimate that area. It is helpful to observe the values of several left and right Riemann sums simultaneously in a table form. The following command produces such a table.

a=0;b=2;
TableForm[Table[{i,leftSum[f,a,b,i,8],rightSum[f,a,b,i,8],Abs[leftSum[f,a,b,i,8]-rightSum[f,a,b,i,8]]},{i,5,75,5}]]

The four columns of the above table list, successively: the number n (5,10,15,...) of subintervals in the partition of the interval [0,2]; the value of the left Riemann sum based on n subitervals, correct to the eighth decimal place; the value of the right Riemann sum based on n subitervals, correct to the eighth decimal place; and the absolute difference between the left and right Riemann sums.

Experiment 14.2

For each of the functions f and the intervals [a,b] given below, do the following:
	(a) Graph the region under the graph y=f(x) between x=a and x=b.
	(b) Use the modules showLeftSums and showRightSums to show a graphical illustrations for approximations of the area of the region R under the graph of f using 10 rectangles.
	(c) Use the modules leftSum and rightSum with a value(s) of n which allows you to estimate the area of R to within 0.001.
	
1. f(x)=(Sqrt[(x^3)+0.1]), on [0,2].
2. f(x)=(((x^2)+1)/(x+3)), on [-2,0]. 
3. f(x)=ln(3 cos x), on [0,1.2].

14.3  Other Riemann Sums

In the definition of the definite integral, the choice of rectangles used to form a Riemann sum need not be as structured as in Sections 14.1 and 14.2. In fact, given any n points (SuperStar[(Subscript[x, 1])]),(SuperStar[(Subscript[x, 2])]),..., (SuperStar[(Subscript[x, n])]) such that (Subscript[x, i-1])≤(SuperStar[(Subscript[x, i])])≤(Subscript[x, i]), we can form a Riemann sum so that the height of the rectangle on the subinterval [(Subscript[x, i-1]), (Subscript[x, i]) ] is f((SuperStar[(Subscript[x, i])])). The next module, named showRiemannSums, illustrates this. This module has the same inputs as showLeftSums and showRightSums above, but it generates random sample points (SuperStar[(Subscript[x, 1])]),(SuperStar[(Subscript[x, 2])]),..., (SuperStar[(Subscript[x, n])]), one for each subinterval, which it then uses to form a Riemann sum. The actual sample points are indicated by dots on the x-axis.

showRiemannSums[f_,a_,b_,n_,k_]:=Module[{g,i,j,h,p1,p2,ξ,η}, 
g[x_]=f[x];
p1=Plot[g[x],{x,a,b},PlotStyle->Thick,PlotRange->All,Frame->True,Axes->False,ImageSize->Medium];
Manipulate[h:=(b-a)/i;ξ=Table[RandomInteger[10]/10,{i}];η=Table[a+j*h,{j,1,i}];
p2=Graphics[
{{LightBlue,Table[Polygon[{{η[[j]]-h,0},{η[[j]],0},{η[[j]],g[η[[j]]-ξ[[j]]*h]},{η[[j]]-h,g[η[[j]]-ξ[[j]]*h]}}],{j,1,i}]},Table[Line[{{η[[j]]-h,0},{η[[j]]-h,g[η[[j]]-ξ[[j]]*h]},{η[[j]],g[η[[j]]-ξ[[j]]*h]},{η[[j]],0},{η[[j]]-h,0}}],{j,1,i}],{PointSize->0.015,Blue,Table[Point[{η[[j]]-ξ[[j]]*h,0}],{j,1,i}]}}];Column[{Show[p1,p2,p1],"Riemann sum:",N[Sum[g[η[[j]]-ξ[[j]]*h]*h, {j,1,i} ],k]}],
{{i,1,"n"},1,n,1}]]

Here is an example of a call to showRiemannSums.

showRiemannSums[f,0,2,10,10]

We should be noted that when the function f(x),a≤x≤b, is monotone, the left and right Riemann sums from Sections 14.1 and 14.2 are the most natural choice of Riemann sums and, in combination, can be used to estimate the integral (∫_( a)^( b))f(x)dx quite efficiently. However, for general, non-monotone functions, they are not much different from the general Riemann sums in the present section. In fact, often a random choice of the sampling points may yield a good approximation to the integral more quickly than a structured choice (such as the left or right sums). As an example, let us consider the function f(x)=cos((x^2)) on [0,π]. We have

	(∫_( 0)^( π))cos((x^2))dx≈0.565694.
	
Using leftSum and rightSum above, we find that the left and right Riemann sums using 100 subintervals are approximately 0.596 and 0.536, respectively.

Clear[f]
f[x_]:=Cos[x^2]
leftSum[f,0,Pi,100,6]
rightSum[f,0,Pi,100,6]

Furthermore, if we experiment with the dynamic commands showLeftSums and showRightSums, we will discover that these are the closest approximations to the exact value of this integral based on left or right Riemann sums. On the other hand, experimentation with showRiemannSums shows that the general Riemann sums with 100 “randomly chosen” sample points typically behave better. A series of 10 experiments resulted in the following values for the “random” Riemann sums on 100 subintervals: 0.5599, 0.5654, 0.5634, 0.5577, 0.5687, 0.5656, 0.5536, 0.5633, 0.5755, 0.5643. You can experiment further with the command below to find other random Riemann sum approximations to this definite integral.

showRiemannSums[f,0,Pi,100,6]

Experiment 14.3a

For each of the functions f and the intervals [a,b] given below, do the following:

	(a) Use the module showRiemannSums to show a graphical illustration for a Riemann sum of f on [a,b] using 4 rectangles.
	(b) The Riemann sum displayed in part i) has the form

		(Subscript[S, 4])=(f((SuperStar[(Subscript[x, 1])]))+f((SuperStar[(Subscript[x, 2])]))+f((SuperStar[(Subscript[x, 3])]))+f((SuperStar[(Subscript[x, 4])])))(Subscript[Δ, 4]) 

Based on your plot in part (a), state the values of (SuperStar[(Subscript[x, 1])]),(SuperStar[(Subscript[x, 2])]),(SuperStar[(Subscript[x, 3])]),(SuperStar[(Subscript[x, 4])]) and (Subscript[Δ, 4]).	

1. f(x)=(((x^2)+1)/((x^2)+3)), on [-1,2].
2. f(x)=((sin x)/((x^2)+1)), on [0,2]. 
3. f(x)=ln(2 cos x+|x|), on [-3,1].

Credits

These laboratories were created by Raouf Boules, Geoff Goodson, Ohoe Kim and Mike O'Leary for use in the Calculus courses of Towson University. Commercial use is prohibited without permission of the authors. Non-commercial use is permitted, provided this credit section is retained.