Taylor Polynomials

This notebook is by Steven Amgott.  Please send any questions or comments to samgott1@swarthmore.edu.  Feel free to use and distribute this notebook, but keep this author information in any copy you use or distribute.

This notebook produces Taylor polynomials for various functions, and plots the polynomials on the same axes as the function.  A sequence of graph of Taylor polynomials of increasing degree is created and can be animated.  A final graph shows all the Taylor polynomials on the same set of axes.

Initialization.  (Can be skipped, if you answer "Yes" to the initialization request.)

The cells in this section are initialization cells, and will be automatically evaluated if you answer "Yes" to the initialization request.  If you do not answer "Yes" to this request, you must evaluate them before creating the graphs in the second section below.

Needs["Graphics`Animation`"]

Eliminating some unnecessary warning messages.  (Should be skipped)

Off[General::"spell"];
Off[General::"spell1"];

(e^x)

The next cell creates the graphs to be animated.  max is the highest degree polynomial to be used.  xmin, xmax, ymin, and ymax give the bounds on the graph window.  You can change the size of the displayed graphs by adjusting the value following the option ImageSize at the end of the cell.

To see the animation, evaluate the cell, select the entire collection of output graphs, and choose Animate Selected Graphics from the Cell menu item.    VCR like controls will appear at the bottom of the notebook window so that you can slow (or speed) the animation, pause it, and do other operations.

Clear[x, xmin, xmax, ymin, ymax, pPlot, fPlot, n, k, 
      max]
max=10;
xmin=-2;
xmax=5;
ymin=-2;
ymax=150;
fPlot = 
        Plot[(E^x), {x, xmin, xmax}, 
          PlotStyle -> {Thickness[0.005]}, 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
pPlot[n_] := 
        Plot[(∑_(k = 0)^n) ((x^k)/(k!)), {x, xmin, xmax}, 
          PlotStyle -> RGBColor[1, 0, 0], 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
Animate[Show[fPlot,pPlot[k],DisplayFunction -> $DisplayFunction,PlotLabel -> "degree = " <> ToString[k],ImageSize->500],{k, 0, max, 1}]

To show all the polynomials on the same set of axes simultaneously, evaluate the next cell.  (This only works after the previous cell has been evaluated.)

Show[Append[Table[pPlot[n], {n, 0, max}], fPlot],PlotLabel -> "All polys",DisplayFunction -> $DisplayFunction,ImageSize->500];

sin(x)

The next cell creates the graphs to be animated.  (2 max +1) is the highest degree polynomial to be used.  xmin, xmax, ymin, and ymax give the bounds on the graph window.  You can change the size of the displayed graphs by adjusting the value following the option ImageSize at the end of the cell.

To see the animation, evaluate the cell, select the entire collection of output graphs, and choose Animate Selected Graphics from the Cell menu item.    VCR like controls will appear at the bottom of the notebook window so that you can slow (or speed) the animation, pause it, and do other operations.

Clear[x, xmin, xmax, ymin, ymax, pPlot, fPlot, n, k, 
      max]
max=10;
xmin=-2 π;
xmax=2  π;
ymin=-2;
ymax=2;
fPlot = 
        Plot[Sin[x], {x, xmin, xmax}, PlotStyle -> {Thickness[0.005]}, 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
pPlot[n_] := 
        Plot[(∑_(k = 0)^n)(((((-1))^k) (x^(2 k + 1)))/((2 k + 1)!)), {x, xmin, xmax}, 
          PlotStyle -> RGBColor[1, 0, 0], 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
Animate[Show[fPlot,pPlot[k],DisplayFunction -> $DisplayFunction,PlotLabel -> "degree = " <> ToString[2  k + 1],ImageSize->500],{k, 0, max, 1}]

To show all the polynomials on the same set of axes simultaneously, evaluate the next cell.  (This only works after the previous cell has been evaluated.)

Show[Append[Table[pPlot[n], {n, 0, max}], fPlot],PlotLabel -> "All polys",DisplayFunction -> $DisplayFunction,ImageSize->500];

cos(x)

The next cell creates the graphs to be animated.  (2 max) is the highest degree polynomial to be used.  xmin, xmax, ymin, and ymax give the bounds on the graph window.  You can change the size of the displayed graphs by adjusting the value following the option ImageSize at the end of the cell.

To see the animation, evaluate the cell, select the entire collection of output graphs, and choose Animate Selected Graphics from the Cell menu item.    VCR like controls will appear at the bottom of the notebook window so that you can slow (or speed) the animation, pause it, and do other operations.

Clear[x, xmin, xmax, ymin, ymax, pPlot, fPlot, n, k, 
      max]
max=10;
xmin=-2 π;
xmax=2  π;
ymin=-2;
ymax=2;
fPlot = 
        Plot[Cos[x], {x, xmin, xmax}, PlotStyle -> {Thickness[0.005]}, 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
pPlot[n_] := 
        Plot[(∑_(k = 0)^n)(((((-1))^k) (x^(2 k)))/((2 k)!)), {x, xmin, xmax}, PlotStyle -> RGBColor[1, 0, 0], 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
Animate[Show[fPlot,pPlot[k],DisplayFunction -> $DisplayFunction,PlotLabel -> "degree = " <> ToString[2  k],ImageSize->500],{k, 0, max, 1}]

To show all the polynomials on the same set of axes simultaneously, evaluate the next cell.  (This only works after the previous cell has been evaluated.)

Show[Append[Table[pPlot[n], {n, 0, max}], fPlot],PlotLabel -> "All polys",DisplayFunction -> $DisplayFunction,ImageSize->500];

ln(x)

The next cell creates the graphs to be animated.   max  is the highest degree polynomial to be used.  xmin, xmax, ymin, and ymax give the bounds on the graph window.  You can change the size of the displayed graphs by adjusting the value following the option ImageSize at the end of the cell.  Note that for this function we are expanding around 1 rather than around 0.

To see the animation, evaluate the cell, select the entire collection of output graphs, and choose Animate Selected Graphics from the Cell menu item.    VCR like controls will appear at the bottom of the notebook window so that you can slow (or speed) the animation, pause it, and do other operations.

Clear[x, xmin, xmax, ymin, ymax, pPlot, fPlot, n, k, 
      max]
max=10;
xmin=.1;
xmax=3;
ymin=-2;
ymax=2;
fPlot = 
        Plot[Log[x], {x, xmin, xmax}, PlotStyle -> {Thickness[0.005]}, 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
pPlot[n_] := 
        Plot[(∑_(k = 1)^n)(((((-1))^(k + 1)) (((x - 1))^k))/k), {x, xmin, xmax}, PlotStyle -> RGBColor[1, 0, 0], 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
Animate[Show[fPlot,pPlot[k],DisplayFunction -> $DisplayFunction,PlotLabel -> "degree = " <> ToString[k],ImageSize->500],{k, 0, max, 1}]

To show all the polynomials on the same set of axes simultaneously, evaluate the next cell.  (This only works after the previous cell has been evaluated.)

Show[Append[Table[pPlot[n], {n, 0, max}], fPlot],PlotLabel -> "All polys",DisplayFunction -> $DisplayFunction,ImageSize->500];

Did you notice a difference between this function and the ones in the earlier sections?  Do the polynomials approach the function everywhere when the degree increases?

Arbitrary functions.

The next cell creates the graphs to be animated.  max is the highest "degree" polynomial to be used.  (It may not be the actual degree of the polynomial, depending on whether or not the coefficient of (((x - a))^max) is zero.)  xmin, xmax, ymin, and ymax give the bounds on the graph window.  f(x) is the function whose Taylor polynomials you wish to produce, and a is the point around which those polynomials are expanded.  You may have to change some (or all) of these values in magenta in order to produce nice graphs to animate.  You can change the size of the displayed graphs by adjusting the value following the option ImageSize at the end of the cell.

To see the animation, evaluate the cell, select the entire collection of output graphs, and choose Animate Selected Graphics from the Cell menu item.    VCR like controls will appear at the bottom of the notebook window so that you can slow (or speed) the animation, pause it, and do other operations.

The next cell creates the graphs to be animated.

Clear[x, xmin, xmax, ymin, ymax, fPlot, pPlot, a, n, i, m, k, 
      max]
a=0;
max=10;
xmin=-2 π;
xmax=2  π;
ymin=-2;
ymax=2;
f[x_]:=Sin[x]
fPlot = 
        Plot[f[x], {x, xmin, xmax}, PlotStyle -> {Thickness[0.005]}, 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];
\(pPlot[n_] := 
        Plot[Evaluate[∑\+\(k = 0\)\%n\(\((\((∂\_{x, k}\ f[x])\) /. x -> a)\) \((x - a)\)\^k\)\/\(k!\)], {x, xmin, xmax}, 
          PlotStyle -> RGBColor[1, 0, 0], 
          PlotRange -> {ymin, ymax}, 
          DisplayFunction -> Identity];\)
Animate[Show[fPlot,pPlot[k],DisplayFunction -> $DisplayFunction,PlotLabel -> "degree = " <> ToString[k],ImageSize->500],{k, 0, max, 1}]

To show all the polynomials on the same set of axes simultaneously, evaluate the next cell.  (This only works after the previous cell has been evaluated.)

Show[Append[Table[pPlot[n], {n, 0, max}], fPlot],PlotLabel -> "All polys",DisplayFunction -> $DisplayFunction,ImageSize->500];