Exam 2 Take Home: Tea time

Do the following to submit with your exam, from section 4.3, pp. 163-167: #3-5, 11, 12, 18 abfg, 21, 22, 25, 40

For problems 3, 4, 5, and 11, write code that works for an arbitrary function, and then apply the code on those problems. Provide me with a script that allows me to (easily!:) reproduce your results.

Preliminaries:

f[x_]:=x*(x-1)*E^x 
mid[f_,a_,b_]:=(b-a)*f[(b+a)/2]
trap[f_,a_,b_]:=(b-a)/2*(f[a]+f[b])
simpsons[f_,a_,b_]:=1/3(2*mid[f,a,b]+trap[f,a,b])
results[g_,a_,b_,exercise_]:=
Module[{},
Print[exercise];
TableForm[{{trap[g,a,b]},{mid[g,a,b]},{simpsons[g,a,b]},{NIntegrate[g[x],{x,a,b}]}},TableHeadings->{{"trap","mid","simpson","Mathematica"},{"estimate"}}]
]

#3-5:

g[x_]:=x Log[x+1]
{a,b}={-0.5,0};
results[g,a,b,"3-5(a)"]

g[x_]:=Log[x+1]
{a,b}={1.0,3.0};
results[g,a,b,"3-5(b)"]

g[x_]:=Cos[x]^2
{a,b}={-0.25,0.25};
results[g,a,b,"3-5(c)"]

g[x_]:=E^Sin[x]
{a,b}={1.0,3.0};
results[g,a,b,"3-5(d)"]

g[x_]:=x^4
{a,b}={1.0,2.0};
results[g,a,b,"3-5(e)"]

Exercise #11-12:

centeredDiff[f_,x0_,h_]:=(f[x0+h]-f[x0-h])/(2h)
results11[g_,x0_,h_,mine_,maxe_,exercise_]:=
Module[{true=N[g'[x0]],estimate=centeredDiff[g,x0,h],factor=-h^2/6},
Print[exercise];
TableForm[{{estimate},{true},{Abs[true-estimate]},{Abs[factor*mine]},{Abs[factor*maxe]}},TableHeadings->{{"centered","Mathematica","abs error","min error","max error"},{"value"}}]
]

g[x_]:=E^x
g'''[x]
{x0,h}={2,0.1};
Plot[g'''[x],{x,x0-h,x0+h}]
{minerror,maxerror}={E^(x0-h),E^(x0+h)};
results11[g,x0,h,minerror,maxerror,"11(a)"]

g[x_]:=Cosh[2x]^2-Sin[x]
g'''[x]
{x0,h}={Pi,0.05};
Plot[g'''[x],{x,x0-h,x0+h}]
{minerror,maxerror}={g'''[x0-h],g'''[x0+h]};
results11[g,x0,h,minerror,maxerror,"11(b)"]

g[x_]:=Log[2x-3]+5x
g'''[x]
{x0,h}={10,1.0};
Plot[g'''[x],{x,x0-h,x0+h}]
{minerror,maxerror}={g'''[x0+h],g'''[x0-h]};
results11[g,x0,h,minerror,maxerror,"11(c)"]

Exercise #18abfg

18(a) -- Answer: O(hf’’(xi))

Clear[f,x0,h,x]
Series[f[2h+x0],{h,0,3}]
Series[f[h+x0],{h,0,3}]
Series[f[x0],{h,0,3}]
Series[(f[x0+2h]-f[x0])/(2h),{h,0,1}]

18(b)

Clear[f,x0,h,x]
Series[f[2h+x0],{h,0,3}]
Series[f[-h+x0],{h,0,3}]
Series[(f[x0+2h]-f[x0-h])/(3h),{h,0,1}]

18(f)

Clear[f,x0,h,x]
Series[f[2h+x0],{h,0,3}]
Series[f[-h+x0],{h,0,3}]
Series[(f[x0+2h]-3f[x0]+2f[x0-h])/(3h^2),{h,0,1}]

18(g)-- Answer: O(hf’’’(xi))

Clear[f,x0,h,x]
Series[f[7h+x0],{h,0,3}]
Series[f[-5h+x0],{h,0,3}]
Series[(5f[x0+7h]-12f[x0]+7f[x0-5h])/(210h^2),{h,0,1}]

Exercise #21

It will have zero error -- the error is proportional to the nth derivative, where n is number of data points. The fifth derivative of f is zero ...

Exercise #22

f[x_]:=3x^3-2x^2+x
h=0.1;
fp=f'[2]
f''[x]
fd=(f[2+h]-f[2])/h
error=Solve[-h/2f''[xi]==fp-fd,xi]

The error of the forward difference formula is O(hf’’(xi)). The second derivative of f(x) is 18x-4.

Exercise #25

(a) rate of convergence is O(h^3)
(b) degree of precision: linear
(c)

f[x_]:=Sin[x]
h=Pi;
x0=0;
estimate=h/2.0(f[x0+h/3]+f[x0+2h/3])
true=NIntegrate[f[x],{x,0,Pi}]
abserror = Abs[true-estimate]

(d) The max size of f’’[x]=-sin(x) on [0,Pi] is 1, and the min is 0. So h^3/36 is a bound on abs error:

f''[x]
N[h^3/36]

(e) Our actual error of 0.720699046351326 is below the bound of 0.861285463341662.

Exercise #40

Clear[p0,p1,p2]
x0=0;
h=1;
xpoints={0,1,2};
p0[x_]:=1.0+x*0 (* Needed this silly second term to get p0 vectorized....*)
p1[x_]:=(x-1)
p2[x_]:=(x-1)^2
int0=Integrate[p0[x],{x,0,2}];
int1=Integrate[p1[x],{x,0,2}];
int2=Integrate[p2[x],{x,0,2}];
m={p0[xpoints],p1[xpoints],p2[xpoints]}; (* without that term, it just produced "1", rather than {1,1,1}! Yikes....*)
b={int0,int1,int2};
MatrixForm[m]
MatrixForm[b]
LinearSolve[m,b]//MatrixForm (* The linear system is simple enough to solve by hand, but I'm lazy!:) *)

So the solution is {a,b,c}={1/3,4/3,1/3} -- that’s Simpson’s rule!