\magnification=\magstep1
\centerline{Mathematics 132 -- Calculus for Physical and Life Sciences 2}
\centerline{Lab Day 1 -- ``In search of a better numerical integral method''}
\centerline{February 21, 2005}
\bigskip
\noindent
{\it Background}
\bigskip
Last Friday in class we discussed the $LEFT(n)$, $RIGHT(n)$, 
$MID(n)$, and $TRAP(n)$ methods for approximating definite integrals
(the left-, right-, and midpoint Riemann sums were not new; the 
trapezoidal method was new).
We have seen the following patterns (some more than once!):
\bigskip
\item{$\bullet$}  If $f$ is {\it increasing} on $[a,b]$, then 
$LEFT(n)$ gives an {\it underestimate} of $\int_a^b f(x)\ dx$ for all $n$.
If $f$ is {\it decreasing} on $[a,b]$, then 
$LEFT(n)$ gives an {\it overerestimate} of $\int_a^b f(x)\ dx$ for all $n$.
\item{$\bullet$}  If $f$ is {\it decreasing} on $[a,b]$, then 
$RIGHT(n)$ gives an {\it undererestimate} of $\int_a^b f(x)\ dx$ for all $n$.
If $f$ is {\it increasing} on $[a,b]$, then 
$LEFT(n)$ gives an {\it overerestimate} of $\int_a^b f(x)\ dx$ for all $n$.
\item{$\bullet$}  Whether $TRAP(n)$ is an under- or over-estimate
of $\int_a^b f(x)\ dx$ depends on the {\it concavity} of $f$.
If $f$ is concave up on $[a,b]$, then $TRAP(n)$ will give
an overestimate of the integral.  If $f$ is concave down on $[a,b]$, 
then $TRAP(n)$ will give an underestimate of the integral.  
\item{$\bullet$}  Whether $MID(n)$ is an under- or over-estimate
of $\int_a^b f(x)\ dx$ also depends on the {\it concavity} of $f$,
and we want to understand this as well.
\bigskip
\noindent
Today, we will gather some data on these methods by looking at
several examples, and introduce an even better method obtained by 
{\it combining two of these methods} in an appropriate way.
\bigskip
\noindent
{\it Maple Commands and Examples}
\bigskip
The commands for finding the left, right, and midpoint sums are 
contained in the student package.  Start by entering
\medskip
\centerline{\tt with(student);}
\medskip
\noindent
to load this.  

The commands we will use in the lab are:
\bigskip
\item{$\bullet$} {\tt leftbox, middlebox, rightbox} which draw
graphical representations of the left-, midpoint, and right-hand 
Riemann sums for a given function, and
\item{$\bullet$} {\tt leftsum, middlesum, rightsum} which
compute the left-, midpoint, and right-hand Riemann sums of a 
given function (as formulas).  For instance, try entering the following
commands to see the pictures for the left- and right-hand sums for 
$f(x) = t^2 - 3t + 4$ on $[a,b]=[0,2]$ with $n = 5$ subdivisions:
\medskip
\centerline{\tt leftbox(t\^{}2 - 3*t + 4, t=0..2,5);}
\centerline{\tt rightbox(t\^{}2 - 3*t + 4, t=0..2,5);}
\medskip
\noindent
To see the numerical values of the left-hand, midpoint, and right-hand sums
(that is $LEFT(5), MID(5)$, and $RIGHT(5)$) 
you can enter commands like this:
\medskip
\centerline{\tt evalf(leftsum(t\^{}2 - 3*t + 4, t=0..2, 5));}
\centerline{\tt evalf(middlesum(t\^{}2 - 3*t + 4, t=0..2, 5));}
\centerline{\tt evalf(rightsum(t\^{}2 - 3*t + 4, t=0..2, 5));}
\medskip
\noindent
There is a similar command for the trapezoidal rule.  This 
does $TRAP(5)$ for the same function as above:
\medskip
\centerline{\tt evalf(trapezoid(t\^{}2 - 3*t + 4, t=0..2, 5));}
\medskip
\noindent
If you leave off the {\tt evalf( )} around the {\tt leftsum}
or {\tt rightsum}, can you see what the output means?

As you can probably guess now, the format for all of these 
commands is: the command name, 
open parenthesis, the formula for the function $f$, comma, $t = $, then the
endpoints, separated by two periods, another comma, then the number 
$n$, followed by the close parenthesis, then the semicolon.

We will also need to be able to get exact values (or at least very
close approximations) to our integrals.  This is done in Maple
by commands like this:
\medskip
\centerline{\tt int(t\^{}2 - 3*t + 4, t=0..2);}
\centerline{\tt evalf(Int(t\^{}2 - 3*t + 4, t=0..2));}
\medskip
\noindent
Try these and look closely at the output.  The first applies the 
FTC and gives the exact value.  The second applies Maple's 
``super-accurate'' numerical methods to give a decimal
approximation that is correct to 8 or 9 
decimal places at least.   (Note the capital I on
the Int here -- it's important, but it's slightly complicated
to explain exactly what it means -- {\it ``don't ask'' unless
you really want to get a peek ``under the hood'' at what 
Maple actually does with your input commands(!)}.)  There will 
be some cases where Maple will not be able to find an antiderivative
of the $f$ you give it; in that case the output will be 
the same integral back again.  For instance try 
\bigskip
\centerline{\tt int(exp(x\^{}3), x=0..1);}
\bigskip
\noindent
This means that Maple was {\it unable to find an elementary 
antiderivative for the function $f(x) = e^{x^3}$}, so it could
not carry out the FTC to find the definite integral.  (In fact
this is an example where no elementary antiderivative exists.)
\bigskip
\noindent
{\it Lab Problems}
\bigskip
\noindent
\item{A)}  For each of the following integrals,
\medskip
\itemitem{1)}  Compute an accurate numerical approximation 
using the {\tt evalf(Int(function, limits)); } command as 
described above.  We will {\it treat this as our exact value} --
it's the most accurate estimate we know!
\itemitem{2)}  Compute $LEFT(n)$, $RIGHT(n)$, $MID(n)$, and
$TRAP(n)$ approximations for $n = 5,10,20,40,80,160$, and 
compute the errors this way:
$${\rm exact\ value} - {\rm approximate\ value}$$
{\it without taking the absolute value, including the 
sign}.  (A negative sign means that the approximate 
value is larger than the exact value, and a positive
sign means that the approximate value is smaller than
the exact value.)  Arrange
your data into {\it three tables} (one table for each of 
the integrals).  In each table,
give the approximation and the error for the
six different $n$ values for each of the four
different methods (LEFT, RIGHT, MID, TRAP).  ({\it Note:}  
Maple has a built-in spreadsheet feature that you can use 
to make these tables.  Look under the Insert pulldown menu 
on the toolbar.  The online Help has information about this 
option.  You can also make the tables ``by hand'' in a text 
region if you prefer.)
\bigskip
\noindent
Integrals:
\bigskip
\item{1)}  $\int_0^2 e^{-x^2/10}\ dx$ (enter the function as {\tt exp(-x\^{}2/10)})
\medskip
\item{2)}  $\int_2^5 {\sin x\over x}\ dx$
\medskip
\item{3)}  $\int_0^1 \sqrt{1 + x^4}\ dx$ (enter the function as {\tt sqrt(1+x\^{}4)})
\bigskip
\item{B)}  Now we want to look for some patterns in our data.  
\bigskip
\itemitem{1)}  For each integral and each method separately,
do you notice any consistent pattern when you compare
the size of the error with a given $n$ and with 
$n$ twice as large (e.g.  the error for $MID(10)$ vs. the error for 
$MID(20)$, or the error for $TRAP(40)$ vs. the error for $TRAP(80)$)?
Is the pattern the same for all of the methods, or does it vary?

\itemitem{2)}  Do you notice any consistent pattern when you 
compare the sizes of the errors for the four different methods on 
the same integral, with the same $n$?  In particular, what is the
approximate relation between the size of the errors for 
the TRAP and MID methods (for the same integral and the same $n$),
and how are the {\it signs} of the two errors related?
\itemitem{3)}  How is the sign of the error for 
$MID(n)$ related to the concavity of $y = f(x)$ on 
the interval $[a,b]$?  You will
probably want to plot the functions to see the concavity!
\bigskip

\item{C)}  One commonly-used better integration method is
called {\it Simpson's Rule}  (no, it's not named for Homer Simpson!)  
One way to 
write the formula for Simpson's rule is:
$$SIMP(n) = {2\cdot MID(n) + TRAP(n)\over 3}$$
There is another command called {\tt simpson}
in the {\tt student} package in Maple
that uses this method to compute approximate values of integrals.
\bigskip
\itemitem{1)}
Try it on the examples from question A, and compare the sizes of the errors
for Simpson's Rule and the other methods for each $n = 5, 10, 20, 40, 80, 160$.  
\itemitem{2)}  Why is Simpson's Rule apparently more accurate?  (Hint:
Think about your answer to part 2 of question B).
\bigskip
\noindent
{\it Assignment}
\bigskip
Individual lab write-ups, due Monday, February 28.


\bye