Summing It up with Riemann, Definite Integrals, and the Fundamental Theorem of Calculus

<<"NumericalCalculus`";
Clear[riemannleft,pulse];
pulse[z_,a_,b_]:=UnitStep[z-a]-UnitStep[z-b];
riemannleft[f_,x_,a_,b_,n_,window_List]:=Block[{floc,xloc,p1,p2},Clear[p1,p2];floc[xloc_]=f/.x->xloc;h=((b-a)/n);fgraph[z_]=(∑_(i=0)^(n-1))floc[a+i h] pulse[z,a+i h,a+(i+1) h];p1=Plot[fgraph[z],{z,a,b},PlotRange->All,PlotStyle->{RGBColor[1,1,1]},Prolog->{Flatten[Table[{Thickness[0.008`],If[h>0,Line[{{a+i h,0},{a+i h,floc[a+i h]},{a+(i+1) h,floc[a+i h]},{a+(i+1) h,0}}],Line[{{a+i h,0},{a+i h,floc[a+(i+1) h]},{a+(i+1) h,floc[a+(i+1) h]},{a+(i+1) h,0}}]],If[floc[a+i h] h≤0,RGBColor[0,0,1],RGBColor[1,0,0]],If[h>0,Polygon[{{a+i h,0},{a+i h,floc[a+i h]},{a+(i+1) h,floc[a+i h]},{a+(i+1) h,0},{a+i h,0}}],Polygon[{{a+i h,0},{a+i h,floc[a+(i+1) h]},{a+(i+1) h,floc[a+(i+1) h]},{a+(i+1) h,0},{a+i h,0}}]],RGBColor[0,0,0]},{i,0,n-1}]]},DisplayFunction->Identity];p2=Plot[floc[xloc],{xloc,a,b},PlotStyle->{RGBColor[0,1,0],Thickness[0.008`]},DisplayFunction->Identity];Show[{p1,p2},ImageSize->{72 6,((72 30)/7)},AspectRatio->(30/42),AxesLabel->{"x or t","y"},DisplayFunction->$DisplayFunction,AxesOrigin->{0,0},PlotRange->window]]

riemannright[f_,x_,a_,b_,n_,window_List]:=Block[{floc,xloc,p1,p2},Clear[p1,p2];floc[xloc_]=f/.x->xloc;h=((b-a)/n);fgraph[z_]=(∑_(i=1)^n)floc[a+i h] pulse[z,a+(i-1) h,a+i h];p1=Plot[fgraph[z],{z,a,b},PlotRange->window,PlotStyle->{RGBColor[1,1,1]},Prolog->{Flatten[Table[{RGBColor[0,0,0],Thickness[0.008`],If[h>0,Line[{{a+(i-1) h,0},{a+(i-1) h,floc[a+i h]},{a+i h,floc[a+i h]},{a+i h,0}}],Line[{{a+(i-1) h,0},{a+(i-1) h,floc[a+(i-1) h]},{a+i h,floc[a+(i-1) h]},{a+i h,0}}]],If[floc[a+i h] h≤0,RGBColor[0,0,1],RGBColor[1,0,0]],If[h>0,Polygon[{{a+(i-1) h,floc[a+i h]},{a+i h,floc[a+i h]},{a+i h,0},{a+(i-1) h,0},{a+(i-1) h,floc[a+i h]}}],Polygon[{{a+(i-1) h,floc[a+(i-1) h]},{a+i h,floc[a+(i-1) h]},{a+i h,0},{a+(i-1) h,0},{a+(i-1) h,floc[a+(i-1) h]}}]]},{i,1,n}]]},DisplayFunction->Identity];p2=Plot[floc[xloc],{xloc,a,b},PlotStyle->{RGBColor[0,1,0],Thickness[0.008`]},DisplayFunction->Identity];Show[{p1,p2},ImageSize->{72 6,((72 30)/7)},AspectRatio->(30/42),AxesLabel->{"x or t","y"},DisplayFunction->$DisplayFunction]]

riemannmiddle[f_,x_,a_,b_,n_,window_List]:=Block[{floc,xloc,p1,p2},Clear[p1,p2];floc[xloc_]=f/.x->xloc;h=((b-a)/n);fgraph[z_]=(∑_(i=1)^n)floc[a+(i-(1/2)) h] pulse[z,a+(i-1) h,a+i h];p1=Plot[fgraph[z],{z,a,b},PlotRange->window,PlotStyle->{RGBColor[1,1,1]},Prolog->{Flatten[Table[{Thickness[0.008`],Line[{{a+i h,0},{a+i h,floc[a+(i+(1/2)) h]},{a+(i+1) h,floc[a+(i+(1/2)) h]},{a+(i+1) h,0}}],If[floc[a+i h] h≤0,RGBColor[0,0,1],RGBColor[1,0,0]],Polygon[{{a+i h,0},{a+i h,floc[a+(i+(1/2)) h]},{a+(i+1) h,floc[a+(i+(1/2)) h]},{a+(i+1) h,0},{a+i h,0}}],RGBColor[0,0,0]},{i,0,n-1}]]},DisplayFunction->Identity];p2=Plot[floc[xloc],{xloc,a,b},PlotStyle->{RGBColor[0,1,0],Thickness[0.008`]},DisplayFunction->Identity];Show[{p1,p2},ImageSize->{72 6,((72 30)/7)},AspectRatio->(30/42),AxesLabel->{"x or t","y"},DisplayFunction->$DisplayFunction]]

Introduction

OBJECTIVE: Visualize the relationship among Riemann sums, the definite integral, and the Fundamental Theorem of the Calculus. Use each of the concepts to compute or approximate the signed area under the graph of a function representing various applications.

In practical applications, the signed area between the graph of a function and the x or t-axis can represent a wide variety of different things. For example, if a function of time represents the rate of water flowing into a storage tank, the total signed area between the graph of the flow rate function and the t-axis is equal to the total amount of water that accumulates or drains from the tank during the time interval. To see that this is the case, you should recall something that you have probably known for a long time, that is, that change in a quantity is equal to its rate of change times time. Some familiar examples of this are "distance is equal to speed times time" and "amount is equal to rate times time." If water flows into a tank at a constant rate of 30 liters per second for 100 seconds, then it easy to determine that the total water added to the tank during this time is 300×100 = 30000 liters. These simple rules are based, of course, on the assumption that the rate of change is constant during the time interval. Now that we are more sophisticated, we know that rates of change do not have to be constant and can, in fact, vary continuously over a time interval. So, how do we calculate the total change over a time interval when the rate varies from one instant to the next? Calculus provides the answer, and we begin by looking at a technique for estimating change from rates of change using a summation process called Riemann sums.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I: An Example

The idea of Riemann sums builds upon what you already know, that is that change is equal to rate of change times time. Suppose that f(t) represents the rate of change of some quantity Q, that is, ((dQ)/dt)=f(t). The quantity Q might represent the amount of water in a reservoir, the distance traveled by a moving object, or the amount of money in an investment portfolio. In these types of practical applications, it is often times easier to consider the forces that influence change and thereby determine f(t) than it is to determine Q directly. But if you know f(t), there is a way to estimate the change in Q over an interval of time, even though the rate of change varies from one instant to the next. Here it is.

Let's consider a specific example. Suppose that (dQ/dt)= f(t) = (1/4)-cos(((π t)/12)) for 0≤ t ≤ 15  and that we wish to calculate ΔQ over this time interval. First, let's graph f(t).

Clear[f,t]
f[t_]=(1/4)-Cos[((π t)/12)];
Plot[f[t],{t,0,15},AxesLabel->{"t","y=f[t]"},PlotStyle->{RGBColor[0,1,0]}]

We know from the definition of differentials that over a short interval of time ΔQ≈ dQ =((dQ)/(dt))dt = f(t) dt. This suggests a method of attack. If we partition the 15-hour period into very short time intervals, called subintervals, and then approximate the change in Q over each of these subintervals and add them up, we should get an approximation of the total ΔQ as t goes from 0 to 15. We let dt be the length or duration of each of the subintervals and evaluate f(t) at some instant in each subinterval. Where should we pick t in each subinterval? If the subintervals are short enough, it shouldn't make too much difference. (You might guess, however, that it would probably be better to pick t somewhere near the middle of each subinterval since that would most likely give a value of f(t) that is near its average value over each subinterval.) 

We can say all of this mathematically as follows. First we divide the total 15-hour period into n subintervals each of length dt and number each subinterval from 1 to n. 

Then, ΔQ = (∑_(i=1)^n)(Subscript[ΔQ, i])≈(∑_(i=1)^n)(Subscript[dQ, i])=(∑_(i=1)^n)f((Subscript[c, i]))h, where h = dt and (Subscript[c, i]) is any value of t taken from the (i^th) subinterval. 

The sum that we form in this way to approximate ΔQ is called a Riemann sum.

There is a geometric view that can be attached to the Riemann sum, (∑_(i=1)^n)f((Subscript[c, i]))h. The product, f((Subscript[c, i]))h, can be thought of as the area of a rectangle that is h units wide and f((Subscript[c, i])) units high. In each subinterval, we can draw this rectangle that is h units wide and f((Subscript[c, i])) units high. The base of each rectangle is on the x or t axis, and the opposite side is on the graph of f(t). The Riemann sum is the sum of the areas of these rectangles. If we admit that f((Subscript[c, i])) could be negative, then the area of a rectangle can be negative; hence, we add signed areas in a Riemann sum, which itself can be positive, negative, or zero. The product can be negative when f((Subscript[c, i])) is positive and h is negative. This would occur if we were to sum back through the time history of f(t). It makes sense that if Q increases as time progresses, then it would decrease if we were to go backwards through the same time interval. And, if Q decreases as time progresses, then it would increase if we were to go backwards through the same time interval.

To illustrate the geometric view of a Riemann sum, we have written a command called riemannleft[f_, t_, a_, b_, n_ ,window_]. The arguments are f, the function f(t), t, the independent variable, a, the left endpoint of the interval, b, the right endpoint of the interval, n, the number of rectangles to be used, and window, the viewing window for the graph. The riemannleft[ ] command uses the left endpoint value in each subinterval for (Subscript[c, i]).

Let's use riemannleft[ ] on the function we have been considering and use 15 rectangles.

riemannleft[f[t],t,0,15,15,{{0,16},{-1,1.5}}]

Note that the blue rectangles have negative areas, whereas the red ones have positive areas. Try changing the number of rectangles in the riemannleft[ ] command to see that the total signed area of the rectangles gets closer to the signed area under the graph of f(t). You can also change the function and the bounds on the interval to explore other possibilities if you wish. See what happens when you reverse the bounds on the time interval.

You Try It: Part I

In addition to riemannleft[ ], we have written riemannright[ ] and riemannmiddle[ ] commands so that you can see the geometry of a Riemann sum when we pick points other than the left endpoint in each subinterval. The arguments are the same as for riemannleft[ ]. Use these commands to visualize the Riemann sums for the function considered in the preceding section and for some other functions that you choose. Here is an example. First, run this function, and then try it out with one of your own by changing the terms in red. Be careful to use correct notation when inserting a function.

Clear[f,x]
f[x]=-3(x^3);
a=-2;
b=2;
n=20;
window={{-2,2},{-24,24}};
riemannleft[f[x],x,a,b,n,window]
riemannmiddle[f[x],x,a,b,n,window]
riemannright[f[x],x,a,b,n,window]

Geometrically, which looks as if it would give the best results? Try it with another function.

Part II: Summing It Up with Riemann

We need to calculate the total signed area of the rectangles. First we calculate h and (Subscript[c, i]) for each of the subintervals, and then we add up the signed areas of the rectangles. We use the left endpoint in each interval for (Subscript[c, i]).

Clear[f,h,a,b,n,t];
f[t_]=(1/4)-Cos[((π*t)/12)];
n=15;
a=0;
b=15;
h=(b-a)/n;
c[i_]=a+i*h;
area[n_]=Sum[f[c[i]]*h,{i,0,n-1}]; (*This is the Riemann sum.*)
Print["The total signed area of the rectangles is ",area[n]//N]

To see what happens as we increase the number of rectangles, we combine the riemannleft[ ] command together with the commands in the preceding cell and put them all in a single cell, the one that follows. With this we can easily change f[t], n, a, and/or b.

Clear[f,a,b,n,h];
f[t_]=(1/4)-Cos[((π*t)/12)];
n=30;
a=0;
b=15;
h=(b-a)/n;
c[i_]=a+i*h;
area[n_]=Sum[f[c[i]]*h,{i,0,n-1}];
Print["The total signed area of the rectangles is ",area[n]//N];
riemannleft[f[t],t,a,b,n,{{0,16},{-1,1.5}}]

If we let the number of subintervals and corresponding rectangles approach infinity, we might expect the Riemann sum to approach the actual signed area under the graph of f(t), provided that f is continuous on [a, b]. The value that the Riemann sum approaches is exactly equal to ΔQ over the interval.

You Try It: Part II

Try out your own function by changing the terms in red. Be careful to use the correct notation. Replace any of the terms in red with functions and domains of your own choosing.

Clear[f,a,b,n,h,x];
f[x_]=(1/(Sqrt[2π]))Exp[-(x^2)/2];
n=20;
a=-2;
b=2;
window={{-3,3},{0,0.5}};
h=(b-a)/n;
c[i_]=a+i*h;
area[n_]=Sum[f[c[i]]*h,{i,0,n-1}];
Print["The total signed area of the rectangles is ",area[n]//N];
riemannleft[f[x],x,a,b,n,window]

Part III: Take it to the Limit - The Definite Integral

To evaluate the limit of the Riemann sum, we form the sum without assigning a specific value to n and then take the limit as n goes to infinity. In the first cell that follows, we use Mathematica to find an expression for the Riemann sum in terms of n, and in the next cell we take the limit.

Clear[f,a,b,n,h];
f[t_]=(1/4)-Cos[((π*t)/12)];
a=0;
b=15;
h=(b-a)/n;
c[i_]=a+i*h;
riemannSum[n_]=Sum[f[c[i]]*h,{i,0,n-1}]//ExpToTrig//Simplify

Now we take the limit.

deltaQ=Limit[riemannSum[n],n->∞]//Simplify

This is the exact value for the signed area under the graph of f(t), and hence it is also the exact value for ΔQ. The limit of a Riemann sum is called a definite integral and is specified by the following notation, 

(∫_a^b)f(t)dt=(Underscript[lim , h->0])(∑_(i=1)^n)f((Subscript[c, i]))h. 

We can use Mathematica to evaluate definite integrals as in the following cell.

deltaQ=(∫_0^15)f[t]\[DifferentialD]t

To compare this result with the values of the Riemann sums calculated above, we express the preceding result as a decimal approximation.

deltaQ//N

It is important to remember that a definite integral is defined as the limit of a Riemann sum. We are sure that the definite integral of a function will exist (i.e., the limit of the Riemann sum will be a finite number), provided that f(t) is continuous on the closed interval over which we perform the summation.

You Try It: Part III

Exercise 1
For each of the following functions, use the group of commands copied into the cell below to estimate the definite integral using a Riemann sum. First use the riemannleft[ ] command to show the graph and some rectangles (use fewer than 50 rectangles for a clear image). Then use the commands in the next cell to see if you can make an estimate of the definite integral that is accurate to 3 digits by varying n. Make note of how many subintervals you need to use to do this. After you make an estimate, use the definite integral command to see Mathematica's decimal estimate of the exact value, and compare it with yours. Here are some functions to try. We show (c) as an example and find by trial and error that 5853 rectangles are needed to get three-digit accuracy.

a)  f(t) = 3(t^2)- 6  for  -1≤ t ≤ 3

b)   f(t) =2 sin(t)   for  -π≤t≤2π

c)   f(t) =(2/(t^2))   for   0.5≤t≤3

d)   f(t) =(Sqrt[t])   for   1 ≤ t ≤ 4

e)   f(t) = (1/t)    for   1≤t≤2

You can change the terms in red to explore the functions listed above.

Clear[f,a,b,n,h,t];
f[t_]=2/(t^2);
a=0.5;
b=3;
n=15;
window={{0,3},{0,8}};
riemannleft[f[t],t,a,b,15,window]

n=5853;
h=(b-a)/n;
c[i_]=a+i*h;
area[n_]=Sum[f[c[i]]*h,{i,0,n-1}];
Print["The total signed area of the rectangles is ",area[n]//N];

(∫_a^b)f[t]\[DifferentialD]t//N

Exercise 2
Write a series of commands to calculate the Riemann sum using the midpoint of each subinterval. Use your series of commands to repeat the problems in exercise 1. Compare the number of rectangles that are required to get three-digit accuracy using the left Riemann sum, and the midpoint Riemann sum and discuss what you find. Use the command riemannmiddle[ ] to visualize the midpoint rectangles if you wish.

Part IV: Sometimes There is an Easier Way - The Fundamental Theorem of Calculus

Remember that our motivation for calculating the area under the graph of a function ((dQ)/(dt))=f(t) is to determine ΔQ over a time interval a ≤ t ≤ b. Our approach has been:

1. to first divide the time interval into a sequence of subintervals,

2. to then approximate Δ(Subscript[Q, i]) over each of these subintervals of time with d(Subscript[Q, i])  = f((Subscript[c, i]))h (i.e., over a short time interval, the change in Q is approximately equal its rate of change times time),

3. to finally add up the d(Subscript[Q, i])'s in a Riemann sum and take the limit of the sum as the number of subintervals goes to infinity.

As the number of subintervals increases, the Riemann sum approximation improves, approaching the exact value for ΔQ.  That is, as the number of subintervals goes to infinity, the value of the Riemann sum approaches the exact value of ΔQ, and this we call the definite integral of f(t) over the interval [a, b].

You are probably wondering, why all the fuss? Wouldn't it be easier simply to find Q = F(t), an antiderivative of f(t), and calculate ΔQ = Q(Subscript[|, t=b])- Q(Subscript[|, t=a])=F(b)-F(a)? Yes, in fact we can do exactly that. This can make the whole business of evaluating a definite integral a lot easier, provided there is a simple formula for the antiderivative, which isn't always the case. Let's do it for the integral (∫_0^15)f(t)dt, where f(t)=(1/4)-cos(((π t)/12)).

Clear[f,a,b,n,h];
f[t_]=(1/4)-Cos[((π*t)/12)];

First, we find an antiderivative of f(t) and then calculate ΔQ.

F[t_]=∫f[t]\[DifferentialD]t+C

Clear[a,b,deltaQ];
deltaQ[a_,b_]=F[b]-F[a]

deltaQ[0,15]

This is the same value we obtained by taking the limit of a Riemann sum! This result is fundamental. It tells us that we can evaluate the definite integral of a function, f(t), by either: 1) taking the limit of a Riemann sum or  2) finding an antiderivative of f(t), let's call it F(t), and taking the difference of this function over the interval of summation. We can summarize this as follows: 

(∫_a^b)f(t)dt=(Underscript[lim , h->0])(∑_(i=1)^n)f((Subscript[c, i]))h=F(b)-F(a) 

or, in terms of Q and (dQ/dt), 

(∫_a^b)(dQ/dt)dt=Q(b)-Q(a). 

This all makes good sense if you think of it as a generalization of our old friend, change is equal to rate times time. This result is so important that it is called the Fundamental Theorem of Calculus, Part 2. The theorem assures us that we can evaluate a definite integral in this simpler way whenever f(t) is continuous over the interval of summation and provided a formula exists for the antiderivative of f(t). 

The Fundamental Theorem of Calculus, Part 2, helps us in two ways. First, as we have discovered, it tells us that instead of taking the limit of a Riemann sum, we can evaluate the definite integral of a function f(t) by taking the difference of values of an antiderivative F(t) over the interval of integration, provided there is a formula for F(t). But what if, as is often the case, there is no algebraic formula for F(t)? Well, now we can turn the problem around. Even when we don't have a formula for F(t), we can evaluate a change in the value of F(t) over an interval [a, b] by taking the limit of a Riemann sum. Specifically, 

F(b) - F(a) =(∫_a^b)f(t)dt=(Underscript[lim , h->0])(∑_(i=1)^n)f((Subscript[c, i]))h. 

This is the second way that the Fundamental Theorem of Calculus, Part 2, helps us out. 

But in this last case, how do we know that the value we calculate is actually a difference of an antiderivative of f(t)? That is, if there is no algebraic formula for F(t), how do we know that the value we calculate by taking the limit of a Riemann sum with f(t) is actually F(b) - F(a), where F(t) is an antiderivative of f(t)? 

The Fundamental Theorem of Calculus, Part 1, provides the answer. It assures us that if f(t) is continuous on [a, b], then f(t) has an antiderivative F(t) that is defined for all t in [a, b] and we can use the definite integral, that is, the limit of a Riemann sum, to form F(t). Specifically, F(t) = (∫_a^t)f(u)du. As a result, we can say for sure that (∫_a^b)f(t)dt=F(b)-F(a), where F(t) is an antiderivative of f(t).

You Try It: Part IV

Now you try repeating the steps in Part IV with the functions in the list that follows. First, calculate the limit of a Riemann sum, and then find an antiderivative and calculate the difference over the interval of summation. Change the limits on the interval (i.e., a and b) as well as the function definition for each exercise. We show (d) as an example. (Note that because Mathematica has difficulty evaluating some limits symbolically, we use the NLimit[ ] command, which gives a numeric estimate of the limit that is precise to the number of digits available in your computer's numeric processor. The Terms->10 option is to ensure accurate estimates of the limit.) 

a)  f(t) = 3(t^2)- 6  for  -1 ≤ t ≤ 3

b)   f(t) =2 sin(t)  for  -π≤t≤2π

c)   f(t) =(2/(t^2))   for   0.5≤t≤3

d)   f(t) =(Sqrt[t])   for   1 ≤ t ≤ 4

Change the terms in red to explore the functions listed above.

Clear[f,a,b,n,h,t];
f[t_]=(Sqrt[t]);
a=1;
b=4;
h=(b-a)/n;
c[i_]=a+i*h;
riemannSum[n_]=Sum[f[c[i]]*h,{i,0,n-1}]//ExpToTrig//Simplify;
NLimit[riemannSum[n],n->∞,Terms->10]

Now we use the Fundamental Theorem.

F[t_]=∫f[t]\[DifferentialD]t

deltaQ=F[b]-F[a]//N

Part V: Calculus Gives Birth to New Functions

The Natural Log Function

As we indicated before, many functions have antiderivatives that cannot be expressed with algebraic formulas. One example is the function f(x)=(1/x). It is important to realize that while the antiderivatives of this function cannot be represented by algebraic formulas, antiderivatives do, nonetheless, exist. We can use the Fundamental Theorem of Calculus, Part 1, to define its antiderivatives, thus giving birth to a new function that can only be defined using calculus. Specifically, an antiderivative of f(x)=(1/x) is defined as F(x)=(∫_1^x)(1/u)du for x>0. Since the function F(x) is so important it is given a special name. It is called the natural log of x and is denoted by ln x. There is a very good reason for calling it a logarithm: it exhibits all of the characteristics of a log function. All of the properties of logarithms hold for ln x.

How do we evaluate this function? While the Fundamental Theorem of Calculus, Part 2, still holds true for f(x)=(1/x) and its antiderivative ln x, that is, 

(∫_a^b)(1/x)dx=ln b-ln a, 

it is not much help in evaluating (∫_a^b)(1/x)dx because there is no algebraic formula for ln x. Consequently, we are forced to resort to its definition to find values for ln x. Specifically, ln x is the limit of a Riemann sum. We know that ln x=(∫_1^x)(1/u)du exists because f(x)=(1/x) is continuous on any interval from 1 to x, provided x>0. But how do we represent the values of ln x as decimals? Usually we settle for an approximation that is precise to some specified number of digits. One approach is to approximate ln x=(∫_1^x)(1/u)du with a Riemann sum using enough rectangles to give the desired precision. Let's do it.

We define our own approximate ln x function in the following cell and call it approxln[x]. The variable n is the number of rectangles used in the Riemann sum that approximates ln x.

approxln[x_,n_]:=Block[{a,b,h,c},
a=1;b=x;
h=(b-a)/n;
c[i_]=a+i*h;
Sum[(1/c[i])*h,{i,0,n-1}]//N];

As an exercise, vary the value of n in the next group of cells to determine how many rectangles are needed to approximate ln x accurate to three decimal places for (1/10)≤x≤10. We start with n=12 and calculate the error, that is, the difference between our approximate value and that given by Log[x], which is Mathematica's command for calculating ln x.

n=12;
approxln[10,n]-Log[10]

approxln[1/10,n]-Log[1/10]

Let's compare our approximating function approxln[x] with Mathematica's Log[x] by graphing the two together. First use n=12, and then replace it with the value that you found above that ensures three decimal places of precision for (1/10)≤x≤10.

n=12;
Plot[{Log[x],approxln[x,n]},{x,0.1`,10},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,1]}},AxesLabel->{"x","y"}]
Print["The natural log function is plotted in red and the approximation to it is plotted in blue."]

What do you observe?

You Try It: The Standard Normal Distribution and the Error Function

When you did the "You Try It: Part II," you may have thought that the function f(x)=(1/(Sqrt[2π]))(e^(-((x^2)/2))) seemed a bit unusual, but the bell-shaped graph that resulted probably looked very familiar. This function is a classic example of one that does not have algebraic forms for its antiderivatives, and it is called the standard-normal-distribution function, which is used in probability and statistics. Let's examine it in more detail.

Clear[f];
f[x_]=(1/(Sqrt[2π]))(E^(-((x^2)/2)));

Plot[f[x],{x,-5,5},PlotRange->All,PlotStyle->{RGBColor[0,1,0]},AxesLabel->{"x","y"}]

Now we consider the definite integral, F(x)=(∫_(-x)^x)f(t)dt=(1/(Sqrt[2π]))(∫_(-x)^x)(e^(-(t^2)/2))dt, which gives the area under the bell-shaped curve between -x and x. Because f(x) is an even function, we can rewrite the integral as F(x)=(Sqrt[(2/π)])(∫_0^x)(e^(-(t^2)/2))dt.  

A function that is closely related to F(x) is the error function, which is denoted by erf x and is defined as follows: erf x=(2/(Sqrt[π]))(∫_0^x)(e^(-(t^2)))dt. We leave it as an exercise for you to show that F(x)=erf( (x/(Sqrt[2]))). We will use F(x) in the remainder of this module. Since there are no algebraic formulas for the antiderivatives of f(x), we will approximate values of F(x) using a Riemann sum with a sufficient number of rectangles to ensure the desired precision. 

Using the method that we used for ln x in Part V above, write Mathematica commands to approximate values of F(x) to three decimal places of accuracy on the interval -4≤x≤4 . First use a left-hand Riemann sum, and then use a midpoint Riemann sum. To test your commands, plot the graph of erf( (x/(Sqrt[2]))) and the graph of your own F(x) to see if the functions match. (In Mathematica, the error function is Erf[x].) Comment on what you find. We illustrate the Riemann sum with the riemannmiddle[ ] command.

riemannmiddle[f[x],x,0,-4,15,{{-4,0},{0,0.4}}]

Note that the signed areas are negative because we are summing from right to left, and therefore h<0, giving f((Subscript[c, i]))h<0 in the sum.

If you need help with this exercise, the following cell, which is closed, contains our solution. Try doing it on your own before you check out what we did.

Solution

Clear[approxF];
approxF[x_,n_]:=Block[{a,b,h,c},
a=0;b=x;
h=(b-a)/n;
c[i_]=a+(i+(1/2))*h;
(Sqrt[(2/π)])*Sum[Exp[-(c[i]^2/2)]*h//N,{i,0,n-1}]];

approxF[-0.1,4]

Erf[-0.1/Sqrt[2]]//N

n=2;
Plot[{approxF[x,n],Erf[(x/(Sqrt[2]))]},{x,-2,2},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,1]}},AxesLabel->{"x","y"}]

There is one final note. Though the use of a Riemann sum to estimate a definite integral is a very direct approach, it is not very efficient. More efficient methods of estimating definite integrals have been developed, and some of these are studied in the module on numerical integration entitled "Riemann, Trapezoids, and Simpson."