Lab session: graphics in Mathematica

Ivan Cnop
Vrije universiteit Brussel
icnop@vub.ac.be

Plotting Taylor polynomials in one variable

Needed

order   n
function   f
point  x0
variable  x
plot interval

Syntax for Series

taylorpoly[n_, f_, x0_, x_] := 
      Normal[Series[f[x], {x, x0, n}]]

Example

taylorpoly[5, Sin, 2, x]

Plotting

graph[f_, left_, right_] := 
      Plot[Evaluate[f[x]], {x, left, right}, PlotStyle -> Hue[1], 
        DisplayFunction -> Identity]

Plotting multiple polynomials

Clear[draw]

draw[n_, f_, x0_, left_, right_] := 
      Table[Plot[Evaluate[taylorpoly[j, f, x0, x]], {x, left, right}, 
          PlotStyle -> Hue[0.2 + 0.05 j], 
          DisplayFunction -> Identity], {j, n}]

Example 1

Square root

n = 12;
f[x] = (Sqrt[x]);
x0 = 2;
l = 0;
r = 10;

draw[n, f, x0, l, r]

Show[Flatten[{graph[f, l, r], %}], 
      DisplayFunction -> $DisplayFunction, 
      AspectRatio -> Automatic]

Other point

n = 12;
f[x] = (Sqrt[x]);
x0 = 3;
l = 0;
r = 10;

draw[n, f, x0, l, r]

Show[Flatten[{graph[f, l, r], %}], 
      DisplayFunction -> $DisplayFunction, 
      AspectRatio -> Automatic]

Compare these two graphs

The second approximation is better on a larger interval.

Example 2

Tangent function

n = 9;
f[x] = Tan[x];
x0 = 0;
l = -2;
r = 5;

draw[n, f, x0, l, r]

Show[Flatten[{graph[f, l, r], %}], 
      DisplayFunction -> $DisplayFunction, 
      AspectRatio -> Automatic, 
      PlotRange -> {-10, 10}]

Animation of taylor polynomials

taylorpoly[n_, f_, x0_, x_] := 
      Normal[Series[f[x], {x, x0, n}]]

Clear[film]

film[n_, f_, x0_, left_, right_] := 
      Do[Show[Flatten[{graph[f, l, r], draw[j, f, x0, l, r]}], 
          DisplayFunction -> $DisplayFunction, 
          PlotRange -> {-3, 5}], {j, n}]

Example 1

n = 12;
f[x] = (Sqrt[x]);
x0 = 2;
l = 0;
r = 10;

film[12, f, 2, 0, 10]

Example 2

n = 12;
f[x] = Sin[x];
x0 = 2;
l = -2;
r = 10;

film[12, f, 2, -2, 10]

Repeat these at other points too