MTH 152

Lab 7

03-09-00

Purpose. The purpose of this lab is to explore the relationship among the various rules that we have for approximating the value of a definite integral. Below is the code for a Maple procedure "compareValues" that will compute approximations to a given definite integral using five different rules and will then arrange the approximations in increasing order. The output also gives the actual value of the integral (to the precision capable in Maple) in the ordered list.

Instructions. First type:

with(student);

to load the student library containing some ot the commands that we need. Next, copy the code appearing between bars at the bottom of this page and paste it into Maple. Hit "return" to enter the code. The procedure that you enter has the following syntax:

compareValues(function, lower_limit, upper_limit, n);

Thus, to approximate the integral of sin(x) on the interval from 0 to Pi/2 using 8 subintervals, you would type:

compareValues(sin(x),0,Pi/2,8);

This would produce the following output:

    Left Endpoint Rule:     1.974231603
    Right Endpoint Rule:    1.974231603
    Trapezoid Rule:         1.974231603
    Actual Value:           2.
    Simpson's Rule:         2.000269170
    Midpoint Rule:          2.012909086
	 	 

Warning: Because the procedure includes Simpson's Rule, you must use an even value of n.

Having entered the code for "compareValues", use this procedure together with the commands leftbox, rightbox, and middlebox to answer the questions below. The command leftbox, for instance, shows a very nice picture for what happens when we use the left endpoint rule. For example, to see what the picture looks like for the integral above (sin(x) fro 0 to Pi/2 with n=8), type:

leftbox(sin(x),x=0..Pi/2,8);

Warning: Note the slight difference in syntax for these commands, namely the "x=0..Pi/2" instead of simply "0,Pi/2".

Question/Tasks

1.

Guess which of the five rules will give the smallest, next smallest, etc, approximation to the integral of x^2 on the interval [0,2] when n=4. Between which two approximations will the actual value of the integral lie? Do you think that the order will change if you let n=20? Now run the procedure and check whether your guesses were correct. Try to explain why things turned out as they did. (Here is where leftbox, middlebox, and rightbox can be very useful.)

2.

In the previous question, Simpson's rule gave the exact value of the integral regardless of the value of n used. Why is this?

3.

Repeat question 1. with the function (1+cos(x)) on the interval [0,8*Pi] and with n=4. Explain why the midpoint rule gives the answer that it does. Explain why all the other rules overestimate the actual value of the integral. Now guess whether Simpson's rule for this integral gives a better approximation with n=6 or with n=8. Check you guess. Are you surprised? Can you explain what is going on?

4.

Finally, consider the function x-ln(x) on the interval [0.5,1,5]. Guess which rule gives the best approximation when n=2. Check you guess and try to explain why things turn out as they do. Now try using larger values of n. Which rule gives the best approximations now?

Code for the procedure "compareValues"

(copy appears between bars and paste into Maple)


 
 compareValues := proc(f,a,b,n) local
 A,B,c,d,i,j;
 
 A:=array(1..6);
 B:=array(1..6);
 
 A[1]:=`    Left Endpoint Rule:  `;
 A[2]:=`    Right Endpoint Rule: `;
 A[3]:=`    Midpoint Rule:       `;
 A[4]:=`    Trapezoid Rule:      `;
 A[5]:=`    Simpson's Rule:      `;
 A[6]:=`    Actual Value:        `;
 B[1]:= evalf(leftsum(f,x=a..b,n));
 B[2]:= evalf(rightsum(f,x=a..b,n));
 B[3]:= evalf(middlesum(f,x=a..b,n));
 B[4]:= evalf(trapezoid(f,x=a..b,n));
 B[5]:= evalf(simpson(f,x=a..b,n));
 B[6]:= evalf(int(f,x=a..b));
 
 for i from 5 by -1 to 1 do
   for j from 1 to i do
     if(B[j]>B[j+1]) then
        c:=B[j]; 
        B[j]:=B[j+1];
        B[j+1]:=c;
        d:=A[j];
        A[j]:=A[j+1];
        A[j+1]:=d;
     fi;
   od;
 od;
 
 for i by 1 to 6 do
   lprint(A[i],B[i])
 od;
 end;
 


Review Examples