# TayPolyn: A Mathematica NotebookTaylor Polynomials and Series in MathematicaCopyright © 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 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 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 generally is a better approximating polynomial. It takes its name from the English mathematician Brook Taylor (1685--1731).

To generate , first impose additional conditions like (1). To produce a polynomial (x) of degree 2 that approximates f near x = a more closely than does T(x) = (x), require not only that (a) = f(a) and '(a) = f '(a) but also that have the same concavity as f at x = a:

(2)                                                  ''(a) = f ''(a).

This is enough to determine a unique polynomial (x) = + (x -- a) + . How? From the condition that (a) = f(a), it follows that

(3)                                                         = f(a).

Since

'(x) = 2(x -- a) + ,

the condition that '(a) = f '(a) gives

(4)                                                   = f '(a).

Similarly, since

''(x) = 2,

it follows from (2) that 2= f ''(a), that is,

(5)                                                      = f ''(a).

Together, (3), (4) and (5) determine a unique formula for (x), namely

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

A similar discussion in case f is differentiable n times at x = a leads to the following formula for the unique nth-degree polynomial (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)    (x)  =  f(a) +  f '(a) (x -- a) + f ''(a) + f '''(a) + ⋯
+
(a) .

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 (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)                                      (x) = 1 -- + -- + .

To see how closely a Taylor polynomial such as (7) actually approximates its corresponding function f, plot the graphs of f and its 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 (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 = (x), first define (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 attaches to an existing function. The semicolon at the end of the first line suppresses printing of the formula for . The special role of the underscore character precludes its use in naming the function , 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 (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 (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 .

``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 (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 (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:  (x), (x), (x), and (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 (x), (x), and (x).)

It is easy to  plot the above four polynomials with the cosine function. The functions (x) and (x) have already been defined, so the following routine defines only (x) and (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 (x), (x), (x) and (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 (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 . For small values of x, such terms are quite small. In fact, the error

(x) = F(x) -- (x)

in approximating F(x) by (x) near the point x = a of expansion approaches 0 as x → a at a rate faster than that at which 0. This generalizes a formula for the error in the tangent approximation: the error (x) in approximating f(x) by (x) near x = a is no greater than half the maximum K of |f ''(x)|  times :

|(x)|  <= K .

Then, |(x)|/|x -- a | <= (1/2)K|x -- a|, so the quotient of (x) by x -- a approaches 0 as x → a. That is, the rate of approach of (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 . 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 (x) to approximate f(x) near x = a is no larger than the product of 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