Examples for Section 8.9
Taylor and Maclaurin Series

Example

Find the Taylor polynomials for f(x)  =  (e^x)at x = 0.

f[x_] :=  (E^x)

The following expression calculates the kth derivative of f(x) evaluated at x = a:

df[k_, a_]  :=  Derivative[k][f][a]

The formula for nth Taylor polynomials of f(x) at x = 0 is:

p[n_, x_]  :=  (∑_(k = 0)^n) df[k, 0]/(k!) (x^k)

Table[p[k, x], {k, 1, 4}]

Mathematica can do this directly with the function Series:

Series[(E^x), {x, 0, 4}]

To convert to a polynomial, apply the function Normal:

Normal[Series[(E^x), {x, 0, 4}]]

Plot[{p[1, x], p[2, x], p[3, x], p[4, x], f[x]},  {x, -2, 2}, 
      PlotStyle -> {Hue[ .3], Hue[ .4], Hue[ .5], Hue[ .6], Hue[ .7]}]

Example

Find the Taylor polynomials for f(x)  =  sin(x)at x = 0.

f[x_] :=  Sin[x]

The definition of the Taylor polynomials is the same as in the previous example (and we'll assume they have been entered).

Table[p[k, x], {k, 1, 7}]

s9 = Normal[Series[Sin[x], {x, 0, 9}]]

Plot[{p[3, x], p[5, x], p[7, x], s9, Sin[x]}, 
       {x, -π, 2  π}, 
      PlotStyle -> {Hue[ .3], Hue[ .4], Hue[ .5], Hue[ .6], Hue[ .7]}]

s25 = Normal[Series[Sin[x], {x, 0, 25}]]

Plot[{Sin[x], s25}, {x, -8 π, 8  π}, 
      PlotStyle -> {RGBColor[0, 0, 1], RGBColor[1, 0, 0]}]

s50 = Normal[Series[Sin[x], {x, 0, 50}]];

Plot[{Sin[x], s50}, {x, -8 π, 8  π}, 
      PlotStyle -> {RGBColor[0, 0, 1], RGBColor[1, 0, 0]}]

p. 677

3.

Find the Taylor polynomials of orders 0, 1, 2, and 3 for f(x) = (1/x)at a = 2.

f[x_]  :=  (1/x)

p0 = f[2]

p1 = f[2] + f'[2] (x - 2)

p2 = 
      f[2] + f'[2] (x - 2) + 
        f''[2]/(2!) (((x - 2))^2)

p3  =  
      f[2] + f'[2] (x - 2) + 
        f''[2]/(2!) (((x - 2))^2) + 
        f'''[2]/(3!) (((x - 2))^3)

\(Plot[{p1, p2, p3, f[x]}, \ {x, 0, 4}, 
	
      PlotStyle -> {Hue[ .4], Hue[ .5], Hue[ .6], Hue[ .7]}]\)

5.

Find the Taylor polynomials of orders 0, 1, 2, and 3 for f(x) = sin(x) at a = (π/4).

f[x_]  :=  Sin[x]

p0 = f[(π/4)]

p1 = f[(π/4)] + f'[(π/4)] (x - (π/4))

p2 = 
      f[(π/4)] + f'[(π/4)] (x - (π/4)) + 
        f''[(π/4)]/(2!) (((x - (π/4)))^2)

p3  =  
      f[(π/4)] + f'[(π/4)] (x - (π/4)) + 
        f''[(π/4)]/(2!) (((x - (π/4)))^2) + 
        f'''[(π/4)]/(3!) (((x - (π/4)))^3)

\(Plot[{p1, p2, p3, f[x]}, \ {x, 0, π\/2}, 
	
      PlotStyle -> {Hue[ .4], Hue[ .5], Hue[ .6], Hue[ .7]}]\)

15.

Find the Maclaurin series for f(x)  =  sin(3  x).

The Maclaurin series for sin(x) is
	\(TraditionalForm
      \`sin(x)\  = \ 
        x\  - \ \(1\/\(3!\)\) x\^3 + \ \(1\/\(5!\)\) x\^5 - 
            \(1\/\(7!\)\) x\^7 +  ... \)
Plugging in 3  x for x gives:
	\(TraditionalForm
      \`sin(3  x)\  = 
        3  x\  - \ \(1\/\(3!\)\) \((3  x)\)\^3 + \ 
            \(1\/\(5!\)\) \((3  x)\)\^5 - \(1\/\(7!\)\) \((3  x)\)\^7 +  ... 
          \)
		=\(TraditionalForm
      \`3  x\  - \ \(3\^3\/\(3!\)\) x\^3 + \ \(3\^5\/\(5!\)\) x\^5 - 
          \(3\^7\/\(7!\)\) x\^7 +  ... \)
		= \(TraditionalForm
      \`3  x\  - \ \(9\/2\) x\^3 + \ \(81\/40\) x\^5 - 
          \(243\/560\) x\^7 +  ... \)

Series[Sin[3  x], {x, 0, 7}]

23.

Find the Taylor polynomial of f(x)  =  (x^4) +  (x^(2 )) + 1 at x = -2.

Since (f^((k)))(x)  =  0 for k ≥ 5, The Taylor polynomial will have terms only up to order 4:

f[x_]  :=  (x^4) +  (x^(2 )) + 1

f[-2] + f'[-2] (x - (-2)) + 
      f''[-2]/(2!) (((x - (-2)))^2) +  
      f'''[-2]/(3!) (((x - (-2)))^3) +  
      f''''[-2]/(4!) 
        (((x - (-2)))^(4 ))

If we were to expand out the above polynomial, we cwould of course come back to the original:

Expand[%]

33.

Find the linear and quadratic approximations (i.e., the Taylor polynomials of order 1 and 2) of f(x)  =  ln(cos(x)) at x = 0.

f[x_]  :=  Log[Cos[x]]

f'[x]

f''[x]

The linear approximation is a horizontal line y = 0:

p1  =  f[0] + f'[0] x

The quadratic approximation is:

p2  =  f[0] + f'[0] x  +  f''[0]/2 (x^2)

Plot[{Log[Cos[x]], p2},  {x, -π/2, π/2},  
      PlotStyle -> {RGBColor[0, 0, 1], RGBColor[1, 0, 0]}]