Taylor Polynomials

The Taylor Polynomials for a function f[x] at a point x=c start out:

p0(x)=f(c)  
p1(x)=f(c) + f'(c)(x-c)
p2(x)=f(c) + f'(c)(x-c) + f''(c)(x-c)^2 / 2!
p3(x)=f(c) + f'(c)(x-c) + f''(c)(x-c)^2 / 2! + f'''(c)(x-c)^3 / 3!

Example 1
Approximate e^x at x=0

Clear[f,p0,p1,p2,p3,p4,x]

f[x_]:=E^x

f[0]
f'[0]
f''[0]
f'''[0]
f''''[0]

p0[x_]:=1
p1[x_]:=1+x
p2[x_]:=1+x+x^2/2
p3[x_]:=1+x+x^2/2+x^3/3!
p4[x_]:=1+x+x^2/2+x^3/3!+x^4/4!

Plot[{f[x],p0[x],p1[x],p2[x],p3[x],p4[x]},{x,-1,2},
  PlotStyle->{RGBColor[0,0,0],RGBColor[1,0,0],
  RGBColor[1,0,0.2],RGBColor[1,0,0.3],
  RGBColor[1,0,0.4],RGBColor[1,0,0.5]}]

Table[{x,p1[x],p2[x],p3[x],p4[x],f[x]},{x,0,1,0.2}];
TableForm[%,TableHeadings->
  {None,{"x","p1","p2","p3","p4","f"}}]

Example 2
Approximate Sin[x] at x=0

Clear[f,p0,p1,p2,p3,p4,p5,x]

f[x_]:=Sin[x]

f[0]
f'[0]
f''[0]
f'''[0]
f''''[0]
f'''''[0]

p0[x_]:=0
p1[x_]:=x
p2[x_]:=x
p3[x_]:=x-x^3/3!
p4[x_]:=x-x^3/3!
p5[x_]:=x-x^3/3!+x^5/5!

Plot[{f[x],p1[x],p3[x],p5[x]},{x,-1,4},
  PlotStyle->{RGBColor[0,0,0],RGBColor[1,0,0],
  RGBColor[1,0,0.2],RGBColor[1,0,0.3],
  RGBColor[1,0,0.4],RGBColor[1,0,0.5]}]

Table[{x,p1[x],p3[x],p5[x],f[x]},{x,0,1,0.2}];
TableForm[%,TableHeadings->
  {None,{"x","p1","p3","p5","f"}}]

Example 3
Approximate Ln[x] at x=1

Clear[f,p0,p1,p2,p3,p4,p5,x]

f[x_]:=Log[x]

f[1]
f'[1]
f''[1]
f'''[1]
f''''[1]
f'''''[1]

p0[x_]:= 0
p1[x_]:= 0+1*(x-1)
p2[x_]:=(0+1*(x-1)-1*(x-1)^2/2)
p3[x_]:=(0+1*(x-1)-1*(x-1)^2/2+2*(x-1)^3/3!)
p4[x_]:=(0+1*(x-1)-1*(x-1)^2/2+2*(x-1)^3/3!
  -6*(x-1)^4/4!)
p5[x_]:=(0+1*(x-1)-1*(x-1)^2/2+2*(x-1)^3/3!
  -6*(x-1)^4/4!+24*(x-1)^5/5!)

Plot[{f[x],p1[x],p2[x],p3[x],p4[x],p5[x]},{x,0.2,3},
  PlotStyle->{RGBColor[0,0,0],RGBColor[1,0,0],
  RGBColor[1,0,0.2],RGBColor[1,0,0.3],
  RGBColor[1,0,0.4],RGBColor[1,0,0.5]}]

Table[{x,p1[x],p3[x],p5[x],f[x]},{x,0.8,2,0.2}];
TableForm[%,TableHeadings->
  {None,{"x","p1","p3","p5","f"}}]

The Series Command

Mathematica has the Series command for computing Taylor polynomials.

Series[Cos[x], {x, 0, 4}]

Plot[{Cos[x], 1-x^2/2+x^4/24}, {x,-1,3},
PlotStyle->{RGBColor[0,0,0],RGBColor[1,0,0]} ]

Notice that the Taylor polynomial for cosine is quite different if we move to a different point!

Series[Cos[x], {x, Pi/2, 4} ]

Plot[{Cos[x], -(-Pi/2+x)+(-Pi/2+x)^3 /6}, {x,-1,6},
PlotStyle->{RGBColor[0,0,0],RGBColor[1,0,0]} ]