166SCW, 7.7 - Example 6: Error Bound Examples Involving Midpoint and Trapezoid Sums

Recall the Mathematica commands for approximation of
b
∫
a
f(x)x
with
M
n
or
T
n
:
In[]:=
MidpointSum[f_,a_,b_,n_]:=
n
∑
i=1
f[a+(2*i-1)*(b-a)/(2*n)]*(b-a)/n
In[]:=
Trapezoid[f_,a_,b_,n_]:=
n
∑
i=1
(1/2)(f[a+(i-1)*(b-a)/n]+f[a+i*(b-a)/n])*(b-a)/n
Let'slookagainatthedefiniteintegral
2
∫
-1
-
2
x
e
x.HowlargemustnbetohaveI-
M
n
<0.001?HowlargemustnbetohaveI-
T
n
<0.001?
In[]:=
f[x_]:=E^(-x^2)
Plot[f[x],{x,-1,2}]
Out[]=
Equation (5) can be used to determine how large n should be. To apply this equation to estimate the error, we need to bound |f '' (x)| on [-1,2]. Just as before in Example 5, here are two ways to do this:
​
​
​
(i) Graphically:
Plot[f''[x],{x,-1,2}]
Out[]=
Clearly,on[-1,2],|f'(x)|≤2,sotake
K
2
=2inEquation(5).Thenitfollowsthat​​ I-
M
n
≤
2*
3
(2+1)
24*
2
n
,sotaking
2*
3
(2+1)
24*
2
n
<0.001
2
n
>
2*
3
(2+1)
24*0.001
n>
2*
3
(2+1)
24*0.001
Sqrt[2*27/(24*.001)]
Out[]=
47.4342
Thus, take n=48.
N[MidpointSum[f,-1,2,48]]
Out[]=
1.62904
Abs[NIntegrate[f[x],{x,-1,2}]-N[MidpointSum[f,-1,2,48]]]
Out[]=
0.00013169
(ii) Using Calculus:​ ​On [-1,2], since f '' is continuous, it must attain its maximum and minimum values. These will occur at either endpoints or critical points of f ''.
f'''[x]
Out[]=
8
-
2
x

x-2x-2
-
2
x

+4
-
2
x

2
x

Solve[f'''[x]==0,x]
Out[]=
{x0},x-
3
2
,x
3
2

Sqrt[3./2]
Out[]=
1.22474
Thus, the places where |f ''| can be maximized on [-1,2] are at x=-1, 0,
3/2
, or x=2:
Abs[f''[{-1,0,Sqrt[3/2],2}]]
Out[]=

2

,2,
4
3/2

,
14
4


N[%]
Out[]=
{0.735759,2.,0.892521,0.256419}
Thus,asabove,weseethat
K
2
=2,sowegetthesameresultsasbefore!
​​For the Trapezoid Sums, using equation (5), an upper bound on the error will be twice as big as that for Midpoint Sums:​​
I-
M
n
≤
2*
3
(2+1)
12*
2
n
,sotaking
2*
3
(2+1)
12*
2
n
<0.001
2
n
>
2*
3
(2+1)
12*0.001
n>
2*
3
(2+1)
12*0.001
Sqrt[2*27/(12*.001)]
Out[]=
67.082
This means that we will need to take n = 68 to get an upper bound on the error that is less than 0.001.
N[Trapezoid[f,-1,2,68]]
Out[]=
1.62877
Abs[NIntegrate[f[x],{x,-1,2}]-N[Trapezoid[f,-1,2,68]]]
Out[]=
0.000131225