Taylor Polynomial Approximations of a Function

taylor[f_,a_,n_]:=Normal[Series[f,{x,a,n}]];
compare2[f_,a_,n_,xlim_,yyylim_]:=Module[{},g=taylor[f,a,n];size=6;graph1=Plot[f,{x,xlim[[1]],xlim[[2]]},AxesLabel->{"x",None},ImageSize->{72 size,((72 size 2)/3)},AspectRatio->(2/3),PlotStyle->{RGBColor[0,0,1]},PlotRegion->{{0,1},{0,0.75`}},DisplayFunction->Identity];graph2=Plot[g,{x,xlim[[1]],xlim[[2]]},PlotStyle->{RGBColor[1,0,0]},ImageSize->{72 size,((72 size 2)/3)},AspectRatio->(2/3),PlotRegion->{{0,1},{0,0.75`}},DisplayFunction->Identity];Show[{graph1,graph2},AspectRatio->(2/3),ImageSize->{72 size,((72 size 2)/3)},PlotRegion->{{0,1},{0,0.75`}},DisplayFunction->$DisplayFunction,PlotRange->{yyylim[[1]],yyylim[[2]]},Epilog->{RGBColor[0,0,1],Text[f,{(1/2) (xlim[[1]]+xlim[[2]]),yyylim[[2]]+0.13` (yyylim[[2]]-yyylim[[1]])}],RGBColor[1,0,0],Text[(Subscript[p, n]),{(1/2) (xlim[[1]]+xlim[[2]]),yyylim[[2]]+0.05` (yyylim[[2]]-yyylim[[1]])}]}]];
taylorpolydemo[f_,a_,iter_,xlimits_,yyylimits_]:=Animate[compare2[f,a,n,xlimits,yyylimits],Evaluate[Flatten[{n,iter},1]]]
Off[Plot::"plnr"];

Introduction

OBJECTIVE: Observe an animated demonstration of the convergence of Taylor polynomials to a function that has derivatives of all orders over some interval of its domain.

Part II of this module contains a specially designed function that can be used for demonstrations and explorations. The taylorpolydemo[ ] command demonstrates the convergence of Taylor polynomials to a function over some interval of its domain, provided the function has derivatives of all orders. The command generates a series of plots showing the function and the Taylor polynomial, as the degree of the polynomial increases. The series of plots that is generated can be animated to demonstrate the convergence of the Taylor polynomials. Several functions are included in the module and others can easily be added. Part I leads you through the construction of the taylorpolydemo[ ] command and explains how it works. If you are only interested in using taylorpolydemo[ ] for demonstrations and explorations, you can skip Part I and the related You Try It section, and go directly to Part II.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I: Computing and Visualizing Taylor Polynomials

We can use Mathematica to find Taylor polynomial approximations for functions in the vicinity of a point whose x-coordinate equals a. The third-degree polynomial that approximates Exp[x] near x = 0 is:

Clear[x]
Series[Exp[x],{x,0,3}]

The last term is called the error term. It tells us that the cubic polynomial gives a fourth-order approximation of Exp[x] near x = 0.

To evaluate a Taylor polynomial for specific values of x, we must obtain a form of the polynomial that does not include the error term. This can be done as follows.

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

Next, we build a Mathematica command that will give an (n^th) degree Taylor polynomial for approximating f near x = a.

taylor[f_,a_,n_]:=Normal[Series[f,{x,a,n}]];

The next command shows how taylor[ ] works.

taylor[Exp[x],0,7]

Now we use our new taylor[ ] command to demonstrate how Taylor polynomials converge on the Sin[x] as n, the degree of the polynomial, increases.

\[MathematicaIcon] About Mathematica

f=Sin[x];
Print["The third degree Taylor polynomial to approximate Sin(x) is ",g=taylor[f,0,3]]
graph1=Plot[f,{x,-2 π,2 π},PlotStyle->{RGBColor[0,0,1]},AxesLabel->{"x","y"},DisplayFunction->Identity];
graph2=Plot[g,{x,-2 π,2 π},PlotStyle->RGBColor[1,0,0],AxesLabel->{"x","y"},DisplayFunction->Identity];
Show[graph1,graph2,PlotRange->{-4,4},DisplayFunction->$DisplayFunction]
Print["The function is plotted in blue; its Taylor polynomial is in red."]

We can put this all together and form a new command, which we will call compare[ ]. When a Mathematica command consists of a list of instructions, we use the Block[ ] command to form the function.

compare[f_,a_,n_,xlimits_,yyylimits_]:=Block[{},g=taylor[f,a,n];size=6;graph1=Plot[f,{x,xlimits[[1]],xlimits[[2]]},PlotStyle->{RGBColor[0,0,1]},AxesLabel->{"x",None},ImageSize->{72 size,((72 size 2)/3)},AspectRatio->(2/3),DisplayFunction->Identity];
graph2=Plot[g,{x,xlimits[[1]],xlimits[[2]]},PlotStyle->{RGBColor[1,0,0]},AxesLabel->{"x",None},ImageSize->{72 size,((72 size 2)/3)},AspectRatio->(2/3),DisplayFunction->Identity];Show[{graph1,graph2},DisplayFunction->$DisplayFunction,PlotRange->{yyylimits[[1]],yyylimits[[2]]},PlotLabel->{f,"versus",(Subscript[p, n])}]]

Let's try it on sin(x) near x = 0.

compare[Sin[x],0,3,{-3*Pi,3*Pi},{-3,3}]

The Animate[ ] command will generate a sequence of graphs showing the convergence of the Taylor polynomials on f near x = a. The value of n will go from 0 to 20 in increments of 2. This means that the sine function will be approximated by polynomials of degree 0 through 20. Notice how each successive approximation does a better job of fitting the function as you move away from x=0.

You can slow, stop, or reverse the animation with the buttons at the top.

Animate[compare[Sin[x],0,n,{-10,10},{-3,3}],{n,0,20,2}]

You Try It: Part I

Try some of your own functions. Some suggestions are: Sin[x] at a=0, Cos[x] at a=0, Exp[x] at a=0, 1/(1+x) at a=0, Log[x] (the natural log) at a=1,and Sqrt[x] at a=4. Change any of the terms in red that you wish. The first cell creates only one Taylor polynomial and graph. The red terms in the next cell will carry out the Taylor approximation for the natural log function around a=1.

f=Log[x];
a=1;
degree=4;
xlimits={-.1,6};
ylimits={-10,3};
Print["The Taylor polynomial to approximate ",f," around ",a," is ",taylor[f,a,degree]]
compare[f,a,degree,xlimits,ylimits]
Print["The function is plotted in blue; its Taylor polynomial is in red."]

To create a series of plots with the function you have chosen, execute the following command.

Animate[compare[f,a,n,xlimits,ylimits],{n,0,20,1}]

The Taylor polynomial seems to approximate the function well in the interval between 0 and 2, but look at what is happening beyond x=2.

Part II: Demonstrations of Convergence and Lack of Convergence

The taylorpolydemo[f_, a_, nrange_, xlimits_, ylimits_ ] command is used to demonstrate the convergence of Taylor polynomials to a function that has derivatives of all orders over some interval of its domain as the degree of the polynomial increases. The arguments are f (a function of x), a (the point about which the function is expanded), nrange (the range of degrees of polynomials to be compared with f, and the increment size), xlimits (the x-axis limits on the graphics window), and ylimits (the y-axis limits on the graphics window). Following are some examples that you can use, or you can add some of your own.

taylorpolydemo[Sin[x],0,{1,19,2},{-4*Pi,4*Pi},{-3,3}]

The sine function you just plotted is very well behaved. Consider the following rational function, and see what happens.

taylorpolydemo[1/(x+1),0,{1,20,1},{-1,2},{-10,30}]

You Try It: Part II

Select your own function, and change the terms in red. Be careful to use correct Mathematica format when inserting your own functions. Some functions that you may want to try, if you haven't already, are: Cos[x] at a=0, Exp[x] at a=0, Log[x] (the natural log) at a=1, and Sqrt[x] at a=4. The commands in the next cell are for Exp[x] at a=0.

f=Exp[x];
a=0;
xlimits={-7,5};
ylimits={-12,48};
nrange={1,14,1};
taylorpolydemo[f,a,nrange,xlimits,ylimits]

\[MathematicaIcon] About Mathematica

Note that in the Plot[ ] and Show[ ] commands, the DisplayFunction -> Identity option turns off the graphics display generated by the command. The DisplayFunction -> $DisplayFunction turns on the graphics display. If an option is not specified, by default the graphics will display. Go back.