Calculus B - Lab 9 

TA: Patrick C. Rowe 

Used with permission of the Authors 

Authors: Jack Wagner & Dr. Karen M Brucks 

Calculus II 

Lesson 20:  Taylor Series 

Definition of Taylor polynomials 

Suppose the nth derivative of Typesetting:-mrow(Typesetting:-mi( is defined at Typesetting:-mrow(Typesetting:-mi( . Then the nth Taylor polynomial for f at Typesetting:-mrow(Typesetting:-mi( is defined as follows: 

 

> p[n](x) = sum((D@@i)(f)(a)/i!*(x-a)^i,i=0..n);
 

p[n](x) = sum(`/`(`*`(((`@@`(D, i))(f))(a), `*`(`^`(`+`(x, `-`(a)), i))), `*`(factorial(i))), i = 0 .. n) (1.1)
 

The Taylor remainder function is defined as Typesetting:-mrow(Typesetting:-mi( 

R[n](x) = abs(`+`(f(x), `-`(p[n](x)))) (1.2)
 

There is a word, taylor, in the Maple vocabulary already which compute Taylor polynomials. Suppose we want the 11 th Taylor polynomial of the sin function at x = 0. 

> p11 := taylor(sin(x),x=0,12);
 

series(`+`(x, `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5))), `-`(`*`(`/`(1, 5040), `*`(`^`(x, 7)))), `*`(`/`(1, 362880), `*`(`^`(x, 9))), `-`(`*`(`/`(1, 39916800), `*`(`^`(x, 1... (1.3)
 

p11 is not actually a polynomial because of the term at the end which is used to signal which polynomial is represented (in case some of the coefficients are 0). We can convert to a polynomial. 

> p11 := convert(p11,polynom);
 

`+`(x, `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5))), `-`(`*`(`/`(1, 5040), `*`(`^`(x, 7)))), `*`(`/`(1, 362880), `*`(`^`(x, 9))), `-`(`*`(`/`(1, 39916800), `*`(`^`(x, 11))))) (1.4)
 

The Taylor polynomials are usually good approximations to the function near a. Let's plot the first few polynomials for the sin function at x =0.  

> sinplot := plot(sin,-Pi..2*Pi,thickness=2):
 

> tays:= plots[display](sinplot):
for i from 1 by 2 to 11 do
tpl := convert(taylor(sin(x), x=0,i),polynom):
tays := tays,plots[display]([sinplot,plot(tpl,x=-Pi..2*Pi,y=-2..2,
color=black,title=convert(tpl,string))]) od:
 

> plots[display]([tays],view=[-Pi..2*Pi,-2..2]);
 

Plot_2d
 

Animation of Taylor series convergence 

The Taylor Polynomials gradually converge to the Taylor Series which is a representation of the original function in some interval of convergence. In this section, we'll see with our own eyes how this convergence takes place in an animation. 

 

First we define a function and a generic Taylor polynomial. Then we define some constant so that our graph displays for a =< x =< b, and c =<y =< d.  

 

> restart; with(plots):
 

> f := x -> sin(x);
 

proc (x) options operator, arrow; sin(x) end proc (2.1)
 

> on := x -> piecewise( x <0, 0, x < 1, 1,1);
 

proc (x) options operator, arrow; piecewise(`<`(x, 0), 0, `<`(x, 1), 1, 1) end proc (2.2)
 

> a := -6: b := 6: c := -3: d:= 4:
 

> g := (x,k) -> convert(series(f(x), x, k), polynom):
 

> display( plot( f(x), x = a..b, y = c..d, thickness = 3, color = blue), animate( on(t-1)*g(x,2) , x = a..b, t = 0..7, view = c..d, color = cyan), animate( on(t-2)*g(x,4) , x = a..b, t = 0..7, view = c..d, color = coral),animate( on(t-3)*g(x,6) , x = a..b, t = 0..7, view = c..d, color = green), animate( on(t-4)*g(x,8) , x = a..b, t = 0..7, view = c..d, color = violet), animate( on(t-5)*g(x,10) , x = a..b, t = 0..7, view = c..d, color = red), animate( on(t-6)*g(x,12) , x = a..b, t = 0..7, view = c..d, color = coral) );
 

Plot_2d
 

>
 

Taylor remainder theorem 

Theorem: (Taylor's remainder theorem) If the (n+1)st derivative of f is defined and bounded in absolute value by a number M in the interval from a to x, then Typesetting:-mrow(Typesetting:-mi(  

This theorem is essential when you are using Taylor polynomials to approximate functions, because it gives a way of deciding which polynomial to use. Here's an example. 

Problem Find the 2nd Taylor polynomial p[2] of Typesetting:-mrow(Typesetting:-mi( at Typesetting:-mrow(Typesetting:-mi(. Plot both the polynomial and f on the interval [.5,1.5]. Determine the maximum error in using p[2] to approximate ln(x) in this interval.  

Solution: 

 

> f := x -> ln(x)*sin(exp(x))+1;
 

proc (x) options operator, arrow; `+`(`*`(ln(x), `*`(sin(exp(x)))), 1) end proc (3.1)
 

> fplot := plot(f,.5..1.5,thickness = 2):
 

> p[2] := x -> sum((D@@i)(f)(1.)/i!*(x-1.)^i,i=0..2);
 

proc (x) options operator, arrow; sum(`/`(`*`(((`@@`(D, i))(f))(1.), `*`(`^`(`+`(x, `-`(1.)), i))), `*`(factorial(i))), i = 0 .. 2) end proc (3.2)
 

> p[2](x);
 

 

`+`(.5892187091, `*`(.4107812909, `*`(x)), `-`(`*`(2.683740378, `*`(`^`(`+`(x, `-`(1.)), 2))))) (3.3)
 

> t2 := unapply( convert(taylor(f(x),x=1,3),polynom),x);
 

proc (x) options operator, arrow; `+`(1, `*`(sin(exp(1)), `*`(`+`(x, `-`(1)))), `*`(`+`(`*`(cos(exp(1)), `*`(exp(1))), `-`(`*`(`/`(1, 2), `*`(sin(exp(1)))))), `*`(`^`(`+`(x, `-`(1)), 2)))) end proc (3.4)
 

> tplot := plot(t2,1..1.5,color=black):
 

> plots[display]([fplot,tplot]);
 

Plot_2d
 

In order to use Taylor's remainder theorem, we need to find a bound M on the 3rd derivative of the function f. In this case, we could just plot the third derivative and eyeball an appropriate value for M. 

> plot((D@@3)(f),.5..1.5) ;
 

Plot_2d
 

We could use M = 75. 

> M := 75;
 

75 (3.5)
 

So the remainder Typesetting:-mrow(Typesetting:-mi( is bounded by  

 

> M/3!*(1.5-1)^3;
 

1.562500000 (3.6)
 

We can see from the plot of f and the polynomial that the actual error is never more than about .1 on the interval [.5,1.5].  

Another example:  

 

Which Taylor polynomial would you use to approximate the sin function on the interval from -Pi to Pi to within 1/10^6? 

Solution:  

Well, 1 is a bound on any derivative of the sin on any interval. So we need to solve the inequality  

> ineq := 1/n!*Pi^n <= 1/10^6;
 

`<=`(`/`(`*`(`^`(Pi, n)), `*`(factorial(n))), `/`(1, 1000000)) (3.7)
 

for n. Solve will not be much help here because of the factorial, but we can find the smallest n by running through a loop. 

> n := 1: while evalf(1/n!*Pi^n) > 1/10^6 do n := n+1 od: print (`take n to be `,n);
 

`take n to be `, 17 (3.8)
 

> (seq(evalf( 1/n!*Pi^n) ,n=15..20));
 

0.2191535349e-4, 0.4303069596e-5, 0.7952054018e-6, 0.1387895250e-6, 0.2294842906e-7, 0.3604730807e-8
0.2191535349e-4, 0.4303069596e-5, 0.7952054018e-6, 0.1387895250e-6, 0.2294842906e-7, 0.3604730807e-8
(3.9)
 

> restart;
 

> t17 := convert(taylor(sin(x),x=0,18),polynom);
 

`+`(x, `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5))), `-`(`*`(`/`(1, 5040), `*`(`^`(x, 7)))), `*`(`/`(1, 362880), `*`(`^`(x, 9))), `-`(`*`(`/`(1, 39916800), `*`(`^`(x, 11)))), ...
`+`(x, `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5))), `-`(`*`(`/`(1, 5040), `*`(`^`(x, 7)))), `*`(`/`(1, 362880), `*`(`^`(x, 9))), `-`(`*`(`/`(1, 39916800), `*`(`^`(x, 11)))), ...
(3.10)
 

> plot(t17,x=-Pi..Pi);
 

Plot_2d
 

 

Looks pretty much like the sin function. 

Problems 

Exercise: Show that Typesetting:-mrow(Typesetting:-mi( is approximated to within 7 decimals by Typesetting:-mrow(Typesetting:-mi( for all x in Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi( . 

Exercise: Use taylor and convert(..,polynom) to compute and plot, on the interval specified, the first few taylor polynomials of the following functions. Observe the convergence of the polynomials to the function and make comments. 

Typesetting:-mrow(Typesetting:-mi( at x=1, on the interval [-1,3]. 

Typesetting:-mrow(Typesetting:-mi( at x =0 on the interval [-2,2] 

Typesetting:-mrow(Typesetting:-mi( at x = 0 on the interval [-2..2] 

 

Exercise: Write a procedure to compute sin(x) for any x by using p[5]. restricted to the interval [0,Pi/4].  

Outline of solution: If x is negative, replace x with Typesetting:-mrow(Typesetting:-mi( and use the oddness Typesetting:-mrow(Typesetting:-mi( property. If x is greater than or equal to 2*Pi, then replace x with x-2*Pi and use the periodicity Typesetting:-mrow(Typesetting:-mi( . Repeat this step until [0, 2*Pi ). If Pi/4 < x < Pi/2 , then use the trig indentity Typesetting:-mrow(Typesetting:-mi( 

and approximate Typesetting:-mrow(Typesetting:-mi( by Typesetting:-mrow(Typesetting:-mi(. If Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(, then Typesetting:-mrow(Typesetting:-mi(. If Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(, then Typesetting:-mrow(Typesetting:-mi(.  

Exercise: Find the smallest n such that the nth Taylor polynomial p[n](x) for Typesetting:-mrow(Typesetting:-mi(at Typesetting:-mrow(Typesetting:-mi( approximates exp(x) to within Typesetting:-mrow(Typesetting:-msup(Typesetting:-mn(for x in [0,1]. (You will want to set Digits equal to 15 in order to do this one. 

or so to do this problem.)