Taylor Series in Mathematica

Marcin Bownik, Gregory D. Landweber
Mathematics Department
University of Oregon

Mathematica's built-in Taylor series commands

Use the Series [ f [x], { x, c, n } ] command to expand the function f(x) in an nth-order Taylor series centered around c. Here is an example:

Series[Sin[x],{x,0,5}]

Mathematica automatically differentiates the function for you and evaluates it at c. In fact, if you give it a generic function, Mathematica simply spits out the general formula for an nth-order Taylor series:

Series[f[x],{x,c,3}]

Series[f[c+h],{h,0,3}]

Notice that Mathematica keeps track of the order of the Taylor series with the last O [ x - c ] term. If you want to strip that term to get a normal polynomial that you can graph, you can feed the output from the Series function into the Normal function as follows:

Normal[Series[Sin[x],{x,0,5}]]

At this point, you might want to look up Taylor series in the Mathematica Help Browser.

Additional Taylor series commands

To make things easier, I have defined below a new Taylor function which works just like Series, but also takes the normal part. I also define a version of Taylor which takes an additional parameter y and evaluates the resulting Taylor series at the point x = y. For numerical computations, I define a NTaylor function which gives a numerical answer.

Taylor[f_,{x_,c_,n_}] := Normal[Series[f,{x,c,n}]]
Taylor[f_,{x_,c_,n_},y_] := Taylor[f,{x,c,n}] /.{x->y}
NTaylor[f_,{x_,c_,n_},y_] := N[Taylor[f,{x,c,n},y]]

Observe how we define functions in Mathematica. We use the assignment operator := instead of the usual equals sign =. This tells Mathematica delay evaluating the expression on the right until it is used later. The underscore after the variable names f_, x_, c_, n_ on the left side indicates to Mathematica that the values f, x, c, n should be substituted into the expression on the right side when it is actually evaluated. Note also my use of the substitution command /. { x -> y }, which substitutes the value y for x in the previous expression.)

Next I define a TaylorError function,which always takes the additional parameteryand measures the difference between the value of the actual function f(y) and the value of the Taylor series when evaluated at y. For numerical calculations, I define a NTaylorError function,which works similarly except that it gives a numerical answer.

TaylorError[f_,{x_,c_,n_},y_] :=(f- Taylor[f,{x,c,n}]) /. {x->y}
NTaylorError[f_,{x_,c_,n_},y_] := N[TaylorError[f,{x,c,n},y]]

For example, if we want to compute sin(3), we could take the 5th order Taylor expansion of sin(x) around the point c = π.

Taylor[Sin[x],{x,Pi,5}]

Evaluating this series at x = 3, we obtain a symbolic value and its numerical counterpart:

Taylor[Sin[x],{x,Pi,5},3]
NTaylor[Sin[x],{x,Pi,5},3]

Computing the error for this series at x = 3, we obtain:

TaylorError[Sin[x],{x,Pi,5},3]
NTaylorError[Sin[x],{x,Pi,5},3]

Notes

Note that we could also compute the Taylor series error manually, by first saving the Taylor series as a function and then subtracting:

f[x_] := Taylor[Sin[x],{x,Pi,5}]
Sin[x]-f[x]

Or even more simply, we could use the Mathematica % symbol, which refers to the result of the previous computation:

Taylor[Sin[x],{x,Pi,5}]
Sin[x] - %
% /. {x->3}
N[%]

If we want to compute the Taylor series error for a general value of x, we can simply pass the TaylorError function the indeterminate x as the value for the final parameter y. For instance:

Taylor[ x^2,{x,2,1}]
TaylorError[x^2,{x,2,1},x]

When Mathematica works with polynomials, it might not simplify them the way you want. You can use the Simplify and Expand functions to get the answers in different forms.

Simplify[Taylor[x^2,{x,2,1}]]
Expand[Taylor[x^2,{x,2,1}]]
Simplify[TaylorError[x^2,{x,2,1},x]]
Expand[TaylorError[x^2,{x,2,1},x]]

Graphing Taylor series

The Mathematica Plot command lets you graph a function. Its syntax is:
	Plot [ f [x], {x, xmin, xmax} ],
where f(x) the function you want to graph, x is the variable paramter, and xmin and xmax specify the range of values for x.
Here is an example:

Plot[Sin[x],{x,-2Pi,2Pi}]

Since you will want to spend some time plotting various Taylor series, I have defined below a new TaylorPlot function which both takes the nth-order Taylor series of the function f(x) at c and graphs the result for the Range of values for x as specified:

TaylorPlot[f_,{x_,c_,n_},Range_]:=
(Plot[Evaluate[Taylor[f,{x,c,n}]],Range])

Here are a couple of examples. Note that as you increase the degree n (which is 5 in the first example and 2 in the second), the graph of the Taylor series looks more and more like the original function farther and father away from the point x = c (which is 0 in the first case and 1 in the second).

TaylorPlot[Sin[x],{x,0,5},{x,-2Pi,2Pi}]

TaylorPlot[Exp[x],{x,1,2},{x,-2,2}]

In addition, here is a TaylorErrorPlot function which graphs the error in the corresponding Taylor series:

TaylorErrorPlot[f_,{x_,c_,n_},Range_] :=
Plot[Evaluate[TaylorError[f,{x,c,n},x]],Range]

Here are the same examples again. Now note that the Taylor series error approaches 0 as you increase the degree in larger and larger regions around x = c.

TaylorErrorPlot[Sin[x],{x,0,5},{x,-2Pi,2Pi}]

TaylorErrorPlot[Exp[x],{x,1,2},{x,-2,2}]

Taylor series animations

The exponential function

Manipulate[ Plot[Evaluate[
{Exp[x],Taylor[Exp[x],{x, -2,n}]}
],{x,-10, 2},
 PlotRange->{{-10,2},{-3,Exp[2]}}], {n,0,20,1}]

The cosine function

Manipulate[ Plot[Evaluate[{Cos[x],Taylor[Cos[x],{x,0,n}]}],{x,-3 Pi, 3Pi}, PlotRange->{{-3 Pi, 3Pi}, {-9,9}}], {n,0,20,1}]

Taylor series error animations

Now we look at animations of the Taylor series error.

The exponential function

Manipulate[ Plot[Evaluate[Exp[x]-Taylor[Exp[x],{x, -2,n}]],{x,-10, 2}, PlotRange->{{-10,2},{-3,Exp[2]}}], {n,1,20,1}]

The cosine function

Manipulate[ Plot[Evaluate[Cos[x]-Taylor[Cos[x],{x,0,n}]],{x,-3 Pi, 3Pi}, PlotRange->{{-3 Pi, 3Pi}, {-9,9}}], {n,1,20,1}]