Taylor Series with Maple

Copyright 2002 by James F. Hurley, University of Connecticut Department of Mathematics, Unit 3009, Storrs CT 06269-3009. All rights reserved.

The interactive version of this Maple worksheet is in MSB 203, in the Math 116 course folder. It illustrates the ideas associated with Taylor series and approximation of functions by Taylor polynomials.

1. Generating Taylor Series. Maple's taylor command generates as many terms n of the Taylor series for an infinitely differentiable function f as you like, about any base point a that you specify. The syntax of the command is

taylor(f(x), x = a, n);

where f(x) is the formula for the function. Execute the following to see the first 10 terms of the exponential series , the Maclaurin series for the exponential function about 0.

> p_9 := taylor(exp(x), x = 0, 10);

p_9 := series(1+1*x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*...

The term
O(x^10) is a built-in order-of-magnitude error estimate. In this case it reports that the size of the error in using the n th-degree Taylor polynomial p[n](x) to approximate f ( x ) = e^x near the base point x = 0 has order of magnitude proportional to x^10 . This illustrates the general remainder estimate for Taylor polynomials (as stated for instance in Theorem 9 of Section 12.10 of James Stewart, Calculus 4th Edtion ). For the exponential function exp, that remainder estimate asserts that for n = 9 an upper bound for the remainder R[n](x) = exp( x ) - p[n](x) is a constant multiple of (x-0)^(n+1) = x^10 . Suppose we denote that constant by delta . Then Maple's output implies that

abs(R[n](x)) < delta*abs(x)^(n+1)/(n+1)! .

From direct application of the ratio test, or Exercise 35(b) of Section 12.6 of Stewart, Calculus 4th Ed . from the Math 116 Syllabus, recall that the series sum(x^n/n!,n = 0 .. infinity) is absolutely convergent for all real numbers x . Hence (from Theorem 6, Section 12.2, p. 743, of Stewart ), limit(x^(n+1)/(n+1)!,n = infinity) = 0. From that and the above inequality (1), it thus follows that limit(R[n](x),n = infinity) = 0, that is, the Taylor series for e^x converges to e^x for all real numbers x . (Compare the foregoing with Example 2, Section 12.10 of Stewart .)

The following Maple routine illustrates how close the Taylor polynomial
p[n](x) is to e^x for x close to the base point 0 of the exponential series. Execute the code to see a plot of the graph of the natural exponential function e^x displayed with that of p[n](x) over the interval [-1, 6].

: the output p_9 of the taylor command is not a polynomial, but rather a data set consisting of p[9](x) with an error estimate tacked on. The second command in the following routine converts that data to p[9](x) . (The first command makes the display command available, which is necessary for displaying graphs of two functions in different colors. Decrease the range of x to see why we did not use a shorter interval of the x -axis.)

> with (plots):
p := convert(p_9, polynom );
graphone := plot ( p, x = -1...6, color = blue):
graphtwo := plot( exp(x), x = -1...6, color = red) :
display(graphone, graphtwo);

Warning, the name changecoords has been redefined

p := 1+x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5+1/720*x...

[Maple Plot]

Observe that only for values of x larger than 4 can the eye detect two colors in the plot. For small values of x it is impossible to distinguish the graphs from the x -axis.

What does the plot suggest about how tables of values of the exponential function are computed? For negative values of
x , or fairly small positive values, the Taylor polynomials of e^x expanded about x = 0 give values very close to e^x . What if you need e^5.9 , or more generally have to study e^x near x = 6? Since Taylor polynomials provide close approximations to their function f ( x ) for values of x close to a , it would be worthwhile to use Taylor polynomials about a nonzero base point a (in this case, a = 6).

In point of fact, in the 18th century the original tables of values for important transcendental functions like exponential and logarithmic functions, sine and cosine functions, etc., were calculated by Taylor-polynomial approximation. Today's calculators and computer compilers use a more sophisticated numerical computation, one that mathematicians and computer scientists developed to provide rapid and accurate function values.

2. Application: approximate integration . For a typical transcendental function f , it is impossible to find a formula for its antiderivative any simpler than int(f(t),t = a .. x) . In such cases, evaluation of a definite integral for f must be carried out by numerical approximation. Especially for hand computation, it is often convenient to approximate int(f(x),x = a .. b) by calculating int(p[n](x),x = a .. b) . As an illustration, consider Exercise 42 of Section 12.10 of Stewart, Calculus, 4th Ed .

42. Approximate int(cos(x^2),x = 0 .. .5) to three decimal places.
Solution. Execute the following Maple routine to produce the Taylor polynomial p[12](x) . (Or, just about as easily, replace x in the Taylor series for cos x by x^2 .)

> p_12 := taylor( cos(x^2), x = 0, 12 );

p := convert( p_12, polynom );

p_12 := series(1-1/2*x^4+1/24*x^8+O(x^12),x,12)

p := 1-1/2*x^4+1/24*x^8

The output provides the first three nonzero terms of the Taylor series for cos x^2 , and reports that the error in approximating cos x^2 by p[11](x) = p[8](x) is proportional to x^12 . For x in the interval [0, 1/2] of integration, that error is thus minuscule. Integration of p[8](x) over [0, 1/2] should therefore provide a close approximation to the definite integral of cos x^2 over that interval. The following Maple routine summarizes the results of that approximation, which is quite simple to carry out by hand with a calculator.

> apprx := int(p, x = 0..1/2);

apprx := 274757/552960



How can we verify that, to the three decimal places asked for in Exercise 42, the value of the given definite integral actually is 0.497? Consider the integral of
p[12](x) over [0, 1/2].

int([1-x^4/2+x^8/24+x^12/720],x = 0 .. 1/2) = 1/2-(1/2)^5/10+(1/2)^9/(9*24)-(1/2)^13/(13*6!) .

Observe that the right side of this expression consists of the first four terms of a convergent alternating series, which arises from term-by-term integration of the Taylor series for cos x^2 . By Theorem 2 of Section 12.9, over the interval [0, 1/2] the series of term-by-term integration of the Taylor series converges to the integral on the left side. The Alternating Series Estimation Theorem guarantees that the error in using the sum s[3] of first three terms to approximate that integral is at most the size of the first term of the alternating series not included in s[3] . That is, the error in using int(p[8](x),x = 0 .. 1/2) to approximate int(cos(x^2),x = 0 .. .5) is at most the size of the last term on the right side above:

R[n](x) 1/13(720)(2^13) 0.000009 < 0.0005.

So the approximation above is indeed accurate to at least three decimal places.