Lab 05: Integrals and Aproximations

Adapted from a lab by Dr. Peter White

Numerical Approximations

Recall in class that we talked about the Midpoint, Trapeziodal, and Simpson’s Methods for approximating Integrals.  As a first step in exploring these methods using Mathematica, execute the following code using  Evaluation/Evaluate Notebook

MidPointMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=Sum[f[x0+(k-1/2)*dx],{k,1,n0}]*dx
]

TrapMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=((f[x0]+f[xn])/2+Sum[f[x0+k*dx],{k,1,n0-1}])*dx
]

SimpMethod[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
output=(f[x0]+f[xn]+4*Sum[f[x0+k*dx],{k,1,n0-1,2}]+2*Sum[f[x0+k*dx],{k,2,n0-2,2}])*dx/3
]

PlotMP[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
part=Table[x0+k*dx,{k,0,n0}];
midpoints=Table[x0+(2*k-1)*dx/2,{k,1,n0}];
p1=Plot[f[x],{x,x0,xn},PlotStyle->{Blue,Thick}];
p2=Graphics[{Table[
{EdgeForm[Black],Opacity[.3],Rectangle[{part[[k]],0},{part[[k+1]],f[midpoints[[k]]]}]},{k,1,n0}]}];
Show[p1,p2,AxesOrigin->{x0,0}]
]

PlotTrap[funct_,a_,b_,n_]:=
Module[{f=funct,x0=a,xn=b,n0=n},
dx=(xn-x0)/n0;
part=Table[x0+k*dx,{k,0,n0}];
p1=Plot[f[x],{x,x0,xn},PlotStyle->{Blue,Thick}];
p2=Graphics[{Table[
{EdgeForm[Black],Opacity[.3],Polygon[{{part[[k]],0},{part[[k]],f[part[[k]]]},{part[[k+1]],f[part[[k+1]]]},{part[[k+1]],0}}]},{k,1,n0}]}];
Show[p1,p2,AxesOrigin->{x0,0}]
]

This code sets up five commands: the first three perform the Midpoint, Trapezoidal, and Simpson’s Methods; the last two generate graphs of the rectangles and trapezoids used in those methods.

Examples of these commands follow:

Clear[f]
f[x_]=1/x

PlotMP[f,1,9,8]

MidPointMethod[f,1,9,8]
N[%,12]

Here we graphed the function f on the interval [1, 9] along with the rectangles used in the Midpoint Method approximation with 8 subintervals.  Then we calculated the approximation and converted it to a 12-digit floating-point number.

Name:  ______________________________     Math 2414 Lab 05

Task

We will now use these commands to approximate (∫_0^2)(e^(-(x^2)))dx .

1.  Define f as a Mathematica function.  Get Mathematica’s best result for this problem with
Clear[f]
f[x_]=Exp[-x^2]
Integrate[f[x],{x,0,2}]
N[%,10]

2.  Generate graphs of the function f showing the rectangles and trapezoids used in the Midpoint and Trapezoidal methods to approximate the integral using 4 subintervals.

PlotMP[f,0,2,4]
PlotTrap[f,0,2,4]

3.  Use these commands to complete the following table. Get 10 significant digits.  Copy and paste your numbers into each row. Change to n = 8, repeat, n=16, repeat. 

n=4
MidPointMethod[f,0,2,n]
N[%,10]
TrapMethod[f,0,2,n]
N[%,10]
SimpMethod[f,0,2,n]
N[%,10]


({{n, Midpoint Method, Trapezoidal Method, Simpson's  Method}, {4, 0.88278894853972780103160600651089843312`10., □, □}, {8, □, □, □}, {16, □, □, □}})

4. What is the maximum theoretical Simpson error S8 from the formula   ((M(((b-a))^5))/(180(n^4)))
 Use n=8 and use the graph to find M.?

Plot[Abs[f’’’’[x]],{x,0,2}]
max=FindMaximum[Abs[f’’’’[x]],{x,0}]
M=max[[1]]
errorS8=N[M*(2-0)^5/(180*8^4)]
   Find the number of partitions for Simpson’s rule so that the maximum theoretical error is less than 0.00001

NSolve[M*(2-0)^5/(180*x^4)==.00001,x,Reals]

n = __________________