Homework, Chapter 5

p. 178, #2, 3
    p. 181, #6
    p. 190, #1, 2, 8
    p. 221, #7, 10

p. 178, #2

Series[Sin[x],{x,0,5}]
g[x_]:=x-Sin[x]
error[x_]:=x^5/120 (* times some derivative of Sine *)
approx[x_]:=x^3/6
absError[x_]:=approx[x]-g[x]
relError[x_]:=absError[x]/g[x]
Plot[absError[x],{x,-.1,.1}]
Plot[relError[x],{x,-.1,.1}]
error[.1]

p. 178, #3

f[x_]:=Exp[x]-1
n=4
a=0
Series[f[x],{x,a,n}]
P[n_,f_,a_,x_]:=HornerForm[Normal[Series[f[t],{t,a,n}]]]/.t->x
(* relative errors  -- need 4th power to get below 10^-5: *)
.1^5/5!*Exp[.1]/f[-.1]
.1^4/4!*Exp[.1]/f[-.1]
Plot[{f[x],P[n,f,a,x]},{x,-.1,.1}]
Plot[{f[x]-P[n,f,a,x]},{x,-.1,.1}]
Plot[(f[x]-P[n,f,a,x])/f[x],{x,-.1,.1}]

p. 181, #6

f[x_]:=Sin[x]
P[n_,f_,a_,x_]:=HornerForm[Normal[Series[f[t],{t,a,n}]]]/.t->x
a=0
Plot[{f[x],P[1,f,a,x],P[3,f,a,x],P[5,f,a,x],P[7,f,a,x]},{x,0,2Pi}]
Plot[{f[x],P[7,f,a,x]},{x,0,2Pi}]

p. 190, #1, 2, 8
    p. 221, #7, 10

Lagrange Polynomial Method -- p. 190, 1a

Clear[x1,x2,x3,y1,y2,y3,a,b,c]

qL[x_]:=y1*(x-x2)(x-x3)/((x1-x2)(x1-x3))+
y2*(x-x1)(x-x3)/((x2-x1)(x2-x3))+
y3*(x-x1)(x-x2)/((x3-x1)(x3-x2))

x1=0
x2=1
x3=2
y1=0
y2=-1
y3=4

Plot[{qL[x]},{x,0,Pi}]

p1=Plot[{qL[x]},{x,Min[{x1,x2,x3}],Max[{x1,x2,x3}]}];
p2=ListPlot[{{x1,y1},{x2,y2},{x3,y3}},PlotMarkers->{Automatic,Large}];
Show[p1,p2]

Lagrange Polynomial Method -- p. 190, 1b

Clear[x1,x2,x3,x4,y1,y2,y3,y4,a,b,c]

qL[x_]:=y1*(x-x2)(x-x3)(x-x4)/((x1-x2)(x1-x3)(x1-x4))+
y2*(x-x1)(x-x3)(x-x4)/((x2-x1)(x2-x3)(x2-x4))+
y3*(x-x1)(x-x2)(x-x4)/((x3-x1)(x3-x2)(x3-x4))+
y4*(x-x1)(x-x2)(x-x3)/((x4-x1)(x4-x2)(x4-x3))

x1=1
x2=3
x3=6
x4=10
y1=1
y2=2
y3=3
y4=4

Plot[{qL[x]},{x,0,Pi}]

p1=Plot[{qL[x]},{x,Min[{x1,x2,x3,x4}],Max[{x1,x2,x3,x4}]}];
p2=ListPlot[{{x1,y1},{x2,y2},{x3,y3},{x4,y4}},PlotMarkers->{Automatic,Large}];
Show[p1,p2]

p. 190, #8:

If the points can be modelled by a lower degree polynomial (e.g. three co-linear points), then the Lagrange polynomial made up of polynomials of degree n+1 will not end up as n+1th degree.

p. 221, #7

dividedDifference[list_]:=
Module[
{n=Length[list],b},
If[n==1,
b=list[[1]][[2]],
b=(dividedDifference[list[[2;;n]]]-dividedDifference[list[[1;;n-1]]])/
(list[[n]][[1]]-list[[1]][[1]])];
Print[list," ",b,"=",N[b]];
b
]
a=dividedDifference[{{3,2}}]
b=dividedDifference[{{3,2},{6,3}}]
c=dividedDifference[{{3,2},{6,3},{1,1}}]
d=dividedDifference[{{1,1},{3,2},{6,3},{10,4}}]
p[x_]:=a+(x-3)*(b+(x-6)*(c+(x-1)*d))
N[p[{1,3,6,10}]]
p1=Plot[{p[x]},{x,1,10}];
p2=ListPlot[{{1,1},{3,2},{6,3},{10,4}},PlotMarkers->{Automatic,Large}];
Show[p1,p2]

p. 221, #10

x1=2.0
x2=2.2
x3=2.4
x4=2.6
x5=2.8
x6=3.0
y1=.30103
y2=.34242
y3=.38021
y4=.41497
y5=.44716
y6=.47712

a=dividedDifference[{{x1,y1}}]
b=dividedDifference[{{x1,y1},{x2,y2}}]
c=dividedDifference[{{x1,y1},{x2,y2},{x3,y3}}]
d=dividedDifference[{{x1,y1},{x2,y2},{x3,y3},{x4,y4}}]
p[x_]:=a+(x-x1)*(b+(x-x2)*(c+(x-x3)*d))
p[2.1]

p1=Plot[{p[x]},{x,x1,x6}];
p2=ListPlot[{{x1,y1},{x2,y2},{x3,y3},{x4,y4}},PlotMarkers->{Automatic,Large}];
Show[p1,p2]

dividedDifference[list_]:=
Module[
{n=Length[list],b},
If[n==1,
b=list[[1]][[2]],
b=(dividedDifference[list[[2;;n]]]-dividedDifference[list[[1;;n-1]]])/
(list[[n]][[1]]-list[[1]][[1]])];
Print[list," ",b];
b
]
dividedDifference[{{x1,y1},{x2,y2},{x3,y3},{x4,y4},{x5,y5},{x6,y6}}];