TayPolyn: A Mathematica Notebook

Taylor Polynomials and Series in Mathematica

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

Mathematica can generate Taylor polynomials [Graphics:Images/TaylorPoly_gr_1.gif]to approximate a function f that has derivatives of every order at a point x = a. Graphing the Taylor polynomials illustrates how close to f(x) the values of [Graphics:Images/TaylorPoly_gr_2.gif] actually are near x = a.

Originally, Taylor polynomials were the means for generating tables of values of such transcendental functions as the sine, cosine and natural logarithm. For many important functions, it is actually feasible to calculate Taylor polynomials by hand. Once you have mastered that process, you can use this notebook to check such work or to generate Taylor polynomials for more complicated functions. Keep in mind that Mathematica isn't available for quizzes or exams, so it is important to become adept at generating the polynomials for simple functions f by hand!

1. Basics. The simplest approximation to a (perhaps complicated) differentiable function f is the tangent approximation, which approximates f(x) by the first-degree (linear) polynomial

T(x)  = f(a) + f '(a) (x -- a).   

T not only agrees with f at x = a, but also has the same slope at that point:

(1)                                                                        T'(x) = f '(a),

for any value of x, including of course x = a.  A natural question arises now:

    •  Is it possible to approximate f more closely by a polynomial of degree > 1?
If f has derivatives of order higher than 1 at x = a then the answer is yes. The nth-degree Taylor polynomial approximation [Graphics:Images/TaylorPoly_gr_3.gif] generally is a better approximating polynomial. It takes its name from the English mathematician Brook Taylor (1685--1731).

To generate [Graphics:Images/TaylorPoly_gr_4.gif], first impose additional conditions like (1). To produce a polynomial [Graphics:Images/TaylorPoly_gr_5.gif](x) of degree 2 that approximates f near x = a more closely than does T(x) = [Graphics:Images/TaylorPoly_gr_6.gif](x), require not only that [Graphics:Images/TaylorPoly_gr_7.gif](a) = f(a) and [Graphics:Images/TaylorPoly_gr_8.gif]'(a) = f '(a) but also that [Graphics:Images/TaylorPoly_gr_9.gif]have the same concavity as f at x = a:

(2)                                                  [Graphics:Images/TaylorPoly_gr_10.gif]''(a) = f ''(a).

This is enough to determine a unique polynomial [Graphics:Images/TaylorPoly_gr_11.gif](x) = [Graphics:Images/TaylorPoly_gr_12.gif][Graphics:Images/TaylorPoly_gr_13.gif]+ [Graphics:Images/TaylorPoly_gr_14.gif](x -- a) + [Graphics:Images/TaylorPoly_gr_15.gif]. How? From the condition that [Graphics:Images/TaylorPoly_gr_16.gif](a) = f(a), it follows that

(3)                                                        [Graphics:Images/TaylorPoly_gr_17.gif] = f(a).


[Graphics:Images/TaylorPoly_gr_18.gif]'(x) = 2[Graphics:Images/TaylorPoly_gr_19.gif](x -- a) + [Graphics:Images/TaylorPoly_gr_20.gif],   

the condition that [Graphics:Images/TaylorPoly_gr_21.gif]'(a) = f '(a) gives

(4)                                                  [Graphics:Images/TaylorPoly_gr_22.gif] = f '(a).

Similarly, since

[Graphics:Images/TaylorPoly_gr_23.gif]''(x) = 2[Graphics:Images/TaylorPoly_gr_24.gif],    

it follows from (2) that 2[Graphics:Images/TaylorPoly_gr_25.gif]= f ''(a), that is,  

(5)                                                     [Graphics:Images/TaylorPoly_gr_26.gif] = [Graphics:Images/TaylorPoly_gr_27.gif]f ''(a).

Together, (3), (4) and (5) determine a unique formula for [Graphics:Images/TaylorPoly_gr_28.gif](x), namely

[Graphics:Images/TaylorPoly_gr_29.gif](x) =  f(a) +  f '(a) (x -- a) + [Graphics:Images/TaylorPoly_gr_30.gif]f ''(a) [Graphics:Images/TaylorPoly_gr_31.gif].

A similar discussion in case f is differentiable n times at x = a leads to the following formula for the unique nth-degree polynomial [Graphics:Images/TaylorPoly_gr_32.gif](x) that agrees with f at x = a and whose first n derivatives have the same values at x = a as do the first n derivatives of f.

Definition. Suppose that a is in the domain of f and its first n derivatives. The nth- degree Taylor polynomial of  f (expanded) about x = a is

(6)    [Graphics:Images/TaylorPoly_gr_33.gif](x)  =  f(a) +  f '(a) (x -- a) + [Graphics:Images/TaylorPoly_gr_34.gif]f ''(a) [Graphics:Images/TaylorPoly_gr_35.gif] + [Graphics:Images/TaylorPoly_gr_36.gif]f '''(a) [Graphics:Images/TaylorPoly_gr_37.gif] + ⋯  
[Graphics:Images/TaylorPoly_gr_38.gif][Graphics:Images/TaylorPoly_gr_39.gif](a) [Graphics:Images/TaylorPoly_gr_40.gif].
The Maclaurin polynomial of f is the Taylor polynomial of f about x = 0 (so involves powers of x rather than x -- a).

The nth-degree Taylor polynomial of f about x = a should be a very close approximation to f, because it has the same value as f at x = a, as well as the same slope, concavity, rate of change of the concavity, etc. This intuitive idea is underscored by the graphical behavior of f and the approximating Taylor polynomials. To illustrate, consider the cosine function about the origin x = 0.
Example. Find [Graphics:Images/TaylorPoly_gr_41.gif](x) for the cosine function, if the base point is 0 and n = 8.

Solution. The first step is to compute the first eight derivatives of the cosine function f, and evaluate them and f at x = 0:

f(x) = cos x         ----->         f(0) = 1  
f '(x) = sin x          ----->        f '(0) = 0
f ''(x) = -- cos x      ----->       f ''(0) = -- 1
f '''(x) = sin x         ----->      f '''(0) = 0
f ''''(x) = cos x         ----->      f ''''(0) = 1.

From these calculations, it is clear that the next four derivatives of f cycle through the same sequence as the first four, so that in particular at 0 they have the respective values 0, --1,  0, and 1. From (6) then, the 8th-degree Taylor polynomial of f about x = 0 is

(7)                                      [Graphics:Images/TaylorPoly_gr_42.gif](x) = 1 -- [Graphics:Images/TaylorPoly_gr_43.gif][Graphics:Images/TaylorPoly_gr_44.gif] + [Graphics:Images/TaylorPoly_gr_45.gif][Graphics:Images/TaylorPoly_gr_46.gif] -- [Graphics:Images/TaylorPoly_gr_47.gif][Graphics:Images/TaylorPoly_gr_48.gif] + [Graphics:Images/TaylorPoly_gr_49.gif][Graphics:Images/TaylorPoly_gr_50.gif].

To see how closely a Taylor polynomial such as (7) actually approximates its corresponding function f, plot the graphs of f and its [Graphics:Images/TaylorPoly_gr_51.gif] for x near a. Mathematica has a command that generates the Taylor polynomial about x = a of any desired degree n for sufficiently differentiable functions f. That command is  

    Series[f[x],{x, a, n}]//Normal

The suffix //Normal puts the Taylor polynomial into normal polynomial form, rather than one that also incorporates information about the order of magnitude of the error in approximating f(x) by [Graphics:Images/TaylorPoly_gr_52.gif](x) near x = a. (See below for a discussion of that error and its order of magnitude.) For example, to check the above results for the cosine function, execute the following command. Try it! (Recall that to execute a command like this, put the cursor at the end of the command and press the Enter key.)

Series[Cos[x],{x, 0, 8}]//Normal

Since 8! = 40320, Mathematica's computation of the eighth-degree Maclaurin polynomial agrees with (7).

To plot the graph of the y = cos and  y = [Graphics:Images/TaylorPoly_gr_54.gif](x), first define [Graphics:Images/TaylorPoly_gr_55.gif](x) as an ordinary function. In Mathematica that requires a subscripted underscore _ after the first occurrence of x in the defining equation. (In the following code, the "delayed equals" symbol := does not appear, because the function name [Graphics:Images/TaylorPoly_gr_56.gif] attaches to an existing function. The semicolon at the end of the first line suppresses printing of the formula for [Graphics:Images/TaylorPoly_gr_57.gif]. The special role of the underscore character precludes its use in naming the function [Graphics:Images/TaylorPoly_gr_58.gif], so the notation below is Pn rather than P_n.) Execute the following two-command routine to see the graph of the cosine function and its 8th-degree Maclaurin polynomial over the interval [-2π, 2π].

Pn[x_] = Series[Cos[x], {x, 0, 8}]//Normal;
Plot[ {Cos[x], Pn[x]}, {x, -2 Pi, 2 Pi},
       PlotStyle -> {GrayLevel [0], RGBColor[1, 0, 1]} ]



Note that between approximately -π and π, there is no visible difference between the plots of the cosine function (black) and [Graphics:Images/TaylorPoly_gr_61.gif](x) (magenta). But for larger values of x there is a clear separation between the two graphs. For smaller values of n, you would expect that the approximation of f by [Graphics:Images/TaylorPoly_gr_62.gif](x) to be less close. That is easy to confirm graphically by slightly modifiying the last routine. Execute the following to see the graph of the cosine and its second-degree Maclaurin polynomial [Graphics:Images/TaylorPoly_gr_63.gif].

P2[x_] = Series[Cos[x], {x, 0, 2}]//Normal;
Plot[ {Cos[x], P2[x]}, {x, -2 Pi, 2 Pi},
       PlotStyle -> {GrayLevel [0], RGBColor[1, 0, 1]} ]



This time, the quadratic approximation [Graphics:Images/TaylorPoly_gr_66.gif](x) to the cosine function has a parabolic graph, which closely approximates the graph of the cosine function only on a shorter interval, roughly between       -π/2 and π/2.

You can also graph y = cos x and several of its approximating Taylor polynomials [Graphics:Images/TaylorPoly_gr_67.gif](x) together on the same set of coordinate axes. A simple way is to define several distinct non-constant Taylor polynomials, for instance those of degree up to 8:  [Graphics:Images/TaylorPoly_gr_68.gif](x), [Graphics:Images/TaylorPoly_gr_69.gif](x), [Graphics:Images/TaylorPoly_gr_70.gif](x), and [Graphics:Images/TaylorPoly_gr_71.gif](x). (These are actually all the distinct Taylor polynomials of degree 8 or less. Since the derivatives of odd degree are all 0 at x = 0, the polynomial of degree 1 is simply the constant polynomial 1, while those of degrees 3, 5, and 7 coincide respectively with the even-degree polynomials [Graphics:Images/TaylorPoly_gr_72.gif](x), [Graphics:Images/TaylorPoly_gr_73.gif](x), and [Graphics:Images/TaylorPoly_gr_74.gif](x).)
It is easy to  plot the above four polynomials with the cosine function. The functions [Graphics:Images/TaylorPoly_gr_75.gif](x) and [Graphics:Images/TaylorPoly_gr_76.gif](x) have already been defined, so the following routine defines only [Graphics:Images/TaylorPoly_gr_77.gif](x) and [Graphics:Images/TaylorPoly_gr_78.gif](x). (Including definitions of the 2nd- and 8th-degree polynomials in this program would generate error messages, so their omission is not just for the sake of avoiding harmless redundancy!) To make it easy to identify the graphs of the individual Taylor polynomials, the RGBColor commands specify readily distinguishable red, green, blue, and magenta curves for the polynomials [Graphics:Images/TaylorPoly_gr_79.gif](x), [Graphics:Images/TaylorPoly_gr_80.gif](x), [Graphics:Images/TaylorPoly_gr_81.gif](x) and [Graphics:Images/TaylorPoly_gr_82.gif](x). Their graphs show clearly how they approximate the black cosine graph progressively more closely as n increases.

P4[x_] = Series[Cos[x], {x, 0, 4}]//Normal;
P6[x_] = Series[Cos[x], {x, 0, 6}]//Normal;
Plot[ {Cos[x], P2[x], P4[x], P6[x], Pn[x]},
      {x, -2 Pi, 2 Pi}, PlotStyle -> {GrayLevel[0],
       RGBColor[1, 0, 0], RGBColor[0, 1, 0],
       RGBColor[0, 0, 1], RGBColor[1, 0, 1]} ]



2. Remainder/Error estimates.
  The steadily improving approximation of the cosine function by its Taylor polynomials [Graphics:Images/TaylorPoly_gr_85.gif](x) as the n increases is typical. The error in approximating the function f by its Taylor polynomial of degree n has order of magnitude roughly of size [Graphics:Images/TaylorPoly_gr_86.gif]. For small values of x, such terms are quite small. In fact, the error

[Graphics:Images/TaylorPoly_gr_87.gif](x) = F(x) -- [Graphics:Images/TaylorPoly_gr_88.gif](x)

in approximating F(x) by [Graphics:Images/TaylorPoly_gr_89.gif](x) near the point x = a of expansion approaches 0 as x → a at a rate faster than that at which [Graphics:Images/TaylorPoly_gr_90.gif]0. This generalizes a formula for the error in the tangent approximation: the error [Graphics:Images/TaylorPoly_gr_91.gif](x) in approximating f(x) by [Graphics:Images/TaylorPoly_gr_92.gif](x) near x = a is no greater than half the maximum K of |f ''(x)|  times [Graphics:Images/TaylorPoly_gr_93.gif]:

|[Graphics:Images/TaylorPoly_gr_94.gif](x)|  <= [Graphics:Images/TaylorPoly_gr_95.gif]K[Graphics:Images/TaylorPoly_gr_96.gif] .

Then, |[Graphics:Images/TaylorPoly_gr_97.gif](x)|/|x -- a | <= (1/2)K|x -- a|, so the quotient of [Graphics:Images/TaylorPoly_gr_98.gif](x) by x -- a approaches 0 as x → a. That is, the rate of approach of [Graphics:Images/TaylorPoly_gr_99.gif](x) to 0 is faster than that of x to a. (Even after division by x -- a, which itself is small since it is approaching 0, the resulting expression still approaches 0 as x → a.)

Mathematica's native Series command provides information about this, because it appends to the Taylor polynomial of degree n an error term in the form O[(x -- a)^(n + 1)]. It indicates that the order of magnitude (represented by the letter O) of the error is the same as the order of magnitude of [Graphics:Images/TaylorPoly_gr_100.gif]. If you don't add the suffix //Normal  in generating the Taylor polynomial of degree 8 for the cosine function, Mathematica reports the order of magnitude of the error term. Execute the following command to see this.

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

The output agrees with a standard remainder estimate, due to Cauchy. That says that the error in using [Graphics:Images/TaylorPoly_gr_102.gif](x) to approximate f(x) near x = a is no larger than the product of [Graphics:Images/TaylorPoly_gr_103.gif] and a constant, namely B/(n + 1)! (where the exclamation point indicates n factorial). Here B is an upper bound for the size of the (n+1)st derivative of f near x = a.

To summarize, Mathematica is a powerful tool for generating Taylor polynomials of many elementary functions. For those functions whose Taylor polynomials are simple to obtain by hand, you can use Mathematica to check your work. For more complicated functions, Mathematica can quickly produce Taylor polynomials that are difficult, or even impossible, to generate by hand.

Converted by Mathematica      April 10, 2002