\magnification=\magstep1
\centerline{Mathematics 134 -- Intensive Calculus for Science 2}
\centerline{Lab Day 2 -- ``In search of a better numerical integration method''}
\centerline{February 27, 2006}
\bigskip
\noindent
{\it Background}
\bigskip
Yesterday 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 {\it was} new).
We have discussed 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 increasing} on $[a,b]$, then 
$LEFT(n)$ gives an {\it undererestimate} 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$}  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 {\it concave up} on $[a,b]$, then 
$TRAP(n)$ gives an {\it overerestimate} of $\int_a^b f(x)\ dx$ for all $n$.
If $f$ is {\it concave down} on $[a,b]$, then 
$TRAP(n)$ gives an {\it undererestimate} of $\int_a^b f(x)\ dx$ for all $n$.
\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$, but
in the opposite way from $TRAP(n)$:
 If $f$ is {\it concave up} on $[a,b]$, then 
$MID(n)$ gives an {\it undererestimate} of $\int_a^b f(x)\ dx$ for all $n$.
If $f$ is {\it concave down} on $[a,b]$, then 
$MID(n)$ gives an {\it overerestimate} of $\int_a^b f(x)\ dx$ for all $n$.
(Recall we saw this by replacing the midpoint rectangles by 
``tangent trapezoids'' with the same areas.)
\bigskip
\noindent
Today, we want to gather some data on these methods by looking at
several examples.  We will also see an {\it even better} method obtained by 
{\it combining two of them} in an appropriate way.
\bigskip
\noindent
{\it Maple Commands and Examples}
\bigskip
The commands for finding the left, right, and midpoint sums are the same
as in Lab 1.  You will need to start by entering the command:
\medskip
\centerline{\tt with(student);}
\medskip
\noindent
to load the student package.  The ones we will use in this time are
just the ones that compute numerical approximations (not the ones
that show the graphics).  For instance,
\medskip
\centerline{\tt evalf(leftsum(x\^{}2 - 3*x + 4, x=0..2, 5));}
\centerline{\tt evalf(rightsum(x\^{}2 - 3*x + 4, x=0..2, 5));}
\centerline{\tt evalf(middlesum(x\^{}2 - 3*x + 4, x=0..2, 5));}
\medskip
\noindent
compute the $LEFT(5), RIGHT(5),$ and $MID(5)$ approximations to 
$\int_0^2 x^2 - 3x + 4\ dx$.
There is a similar command for the trapezoidal rule.  This 
does $TRAP(5)$ for the same function as above:
\medskip
\centerline{\tt evalf(trapezoid(x\^{}2 - 3*x + 4, x=0..2, 5));}
\medskip
\noindent
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$ as a function 
of $x$, comma, $x = $, 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 i.e. ``don't ask'' unless
you really want to get a peak ``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.)
To approximate that integral, we would use
\bigskip
\centerline{\tt evalf(Int(exp(x\^{}3), x=0..1));}
\bigskip
\noindent
{\it A Worked Example}
\bigskip
Suppose we want to compare the $TRAP(20)$ approximation
to $\int_0^1 e^{x^3}\ dx$ with the exact value.
Here is a Maple session illustrating a good way to 
organize the computation.  First assign the name $f$
to the function (so we don't need to retype the formula
over and over):
\bigskip
\centerline{\tt f:=exp(x\^{}3);}
\bigskip
\noindent
Then we compute the trapezoidal rule approximation, and
assign it a name ({\tt trap20}):
\bigskip
\centerline{\tt
trap20:=evalf(trapezoid(f,x=0..1,20));}
\bigskip
\noindent
We take the results from Maple's ``super-accurate''
numerical method as the exact value:
\bigskip
\centerline{\tt
exact:=evalf(Int(f,x=0..1));}
\bigskip
\noindent
Then we use Maple as a numerical calculator to compute
the error for the approximation (exact value) - (approx. value):
\bigskip
\centerline{\tt
err:=exact - trap20;}
\bigskip
\noindent
The error here should be $-0.001696932$.  The size
here indicates that the first three decimal places
of the approximate value are correct (if we round).  
\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 = 20,40,80,160,320$, and 
compute the error for each.  For each integral, arrange
your data into a table, giving the approximation and the error for 
the four methods and the five different $n$ values.  
Format:  Use one row in your table for each
method, and put in columns for the values for each $n$ and the 
associated error (with $n$ increasing left to right).
\bigskip
\noindent
Integrals:
\bigskip
\item{1)}  $\int_0^2 e^{-x^2/2}\ dx$ (enter the function as {\tt exp(-x\^{}2/2)})
\medskip
\item{2)}  $\int_1^2 {\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(20)$ vs. the error for 
$MID(40)$, or the error for $TRAP(160)$ vs. the error for $TRAP(320)$)?
Is the pattern the same for all of the methods, or does it vary?
{\it NOTE:  you may need to ignore the first entry for each method 
before the patterns become apparent!}
\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$)?  
\bigskip

\item{C)}  One commonly-used better integration method is
called {\it Simpson's Rule} (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$.  
\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
One lab write-up from each pair, due Wednesday, March 1.


\bye