<< Graphics`

Calculus II - Bonus Lab:
Taylor Series

Note:  In class I said this lab would consist of two parts (this was to be the second part), but I realized that the other part I was planning for you to use really wasn't workable for you, so this is it...

Taylor Series

To work with Taylor series in Mathematica, we first have to define the function we want to approximate and the "center" of the interval where we want to approximate it:

f[x_] := Log[x]; c = 2;

Now, if we want to work with finite Taylor polynomials, Mathematica has a built in function for that called Series.  There is a catch, however:  If you just type:

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

Mathematica gives you the Taylor series for f(x) expanded around c (c = 1 here) of degree 5.  However, notice the last term:  O[x - 1]^6  This means the "error" term is "on the order of" (((x - 1))^6).  This is cool, but not very useful when you need to plug in a number:

Series[f[x], {x, c, 5}] /. x -> 3

Notice that the way to "plug in" x = 3 is to use the /. x -> 3 notation.  However, notice that it still didn't give you an answer.  This is because of this extra "error term".  To make your series usable, you need to make it a "normal" polynomial:

Normal[Series[f[x], {x, c, 5}]]

Now, you can plug in x = 3:

Normal[Series[f[x], {x, c, 5}]] /. x -> 3 // N

Or graph it:

Plot[Evaluate[Normal[Series[f[x], {x, c, 5}]]], {x, 0, 4}]

(Notice the Evaluate command we also had to add in; you must do this whenever you graph a Taylor series.)

Let's graph f(x) and the Taylor series together on the same graph:

Plot[
      Evaluate[{f[x], Normal[Series[f[x], {x, c, 5}]]}], {x, 0, 
        6}]

If we want to generate a bunch of Taylor polynomials and graph them along with the original function, we can use the Table function:

fred = 
      Table[Plot[
          Evaluate[{f[x], Normal[Series[f[x], {x, c, k}]]}], {x, 0, 6}, 
          PlotRange -> {-5, 3}], {k, 3, 13, 2}]

The way this works is the term {k, 3, 13, 2} that sets k equal to 3 through 13, counting by 2 (so k = 3,  k = 5,  k = 7,  … k = 13), where k is the degree of the Taylor series.  (I use the PlotRange command so that all the graphs show the same range on the Y axis, to make them easier to compare.  If you want hours of harmless amusement, double-click on one of these graphs and it will animate the whole set...)  If you would like to show all of these together on a single graph, you could use the Show command (remember, "fred" is the name we gave to this set of graphs):

Show[fred]

Another graph that might be useful to look at is |f(x) - (Subscript[T, 5])(x)|, where (Subscript[T, 5])(x) is the (5^th) degree Taylor series  (you take the absolute value with Abs).:

actualError := Abs[f[x] - Normal[Series[f[x], {x, c, 5}]]]

Plot[Evaluate[actualError], {x, 0, 6}]

If we want to examine how good an approximation the (5^th)degree Taylor polynomial is on the interval 1 ≤ x ≤ 3, we could just look at the graph of the error on that interval:

Plot[Evaluate[actualError], {x, 1, 3}, 
      PlotRange -> All]

We need the PlotRange->All command so Mathematica will show us everything (if you leave it out, Mathematica tries to be smart and figures there is a vertical asymptote there).  So, it looks like the (5^th)degree Taylor polynomial is accurate to within about 0.005 on this interval.  What does our official error formula tell us?  It says, for a (5^th)degree Taylor polynomial, the error should be less than:

\(TraditionalForm\`\(\(Error\)\(≤\)\) | \(M(x - 2)\)\^6\/\(6!\) | \) for this situation, where M is the maximum value of the (6^th)derivative on the given interval.  Now, we could possibly figure that out exactly (depending on how complicated f(x) is), but the details of that get a little too complicated for what I want to cover in this lab.  Let's just estimate it by looking at the graph of |(f^((6)))(
          x)| on the interval (and using the trace feature to estimate the maximum).  Remember, to take the (6^th)derivative in Mathematica, you use the D command:  D[f[x], {x, 6}]

D[f[x], {x, 6}]

Plot[Evaluate[Abs[D[f[x], {x, 6}]]], {x, 1, 3}, 
      PlotRange -> All]

So, it looks like M ≃ 120.  Therefore, the error formula becomes:

errorTaylor = Abs[((120 (((x - 2))^6))/(6!))]

So, on the entire interval, the largest error could occur at the endpoints:

errorTaylor /. x -> 3.

errorTaylor /. x -> 1.

Perhaps a more interesting thing to do is to graph the error formula over the desired interval:

Plot[errorTaylor, {x, 1, 3}, PlotRange -> All]

Now, let's plot the actual error (red) and the Taylor error formula (yellow) on the same graph to compare:

Plot[Evaluate[{actualError, errorTaylor}], {x, 1, 3}, 
      PlotRange -> All, PlotStyle -> {Red, Yellow}]

So, since the Taylor error formula is so much larger than the true error, what is the point in even using it?

Is it safe to use Taylor's error formula, since it is so "inaccurate"?

If you were to look at larger intervals, do you think this same pattern would hold (i.e., Taylor's error is much bigger than the true error)?  Check your guess by looking at 0.5 ≤ x ≤ 3.5 and 0 ≤ x ≤ 4.  What happens on the interval -1 ≤ x ≤ 5?  (Hint:  remember that M will change on these intervals, when computing Taylor's error.  Show me the work and graphs for these intervals.)

If you were to use a higher degree Taylor polynomial, do you think the same pattern would hold as well (over the original interval 1 ≤ x ≤ 3)?  Check your guess by looking at (Subscript[T, 10])(x) and (Subscript[T, 30])(x).  (When I was writing this problem, I originally wanted to ask you to look at (Subscript[T, 100])(x), but this causes a very strange problem.  Look at this case and explain why you think the error behaves like it does.  Hint: it is very bizarre.)

Consider the function sin((e^x)) near x = 0:

Find the maximum error on the interval -2 ≤ x ≤ 2 for a (3^rd) degree Taylor polynomial for this function.

What is the maximum error for a (5^th)degree Taylor polynomial for this function?  What is odd about this answer?

What is the maximum error for an (11^th)degree Taylor polynomial for this function?

Do you think these last two answers constitute a trend, or just a bizarre fluke?  Examine the maximum error of several higher order Taylor polynomials to help you decide.  (Show me your work here.)

Carefully examine graphs of sin((e^x)) and the Taylor polynomial for a (3^rd),  (5^th),  (11^th), and a few of the higher order Taylor polyomials you found above to explain why the maximum errors seem surprising (at least for the  (5^th) and (11^th)degree polynomials).

What degree Taylor polyomial would you need to use to ensure that the maximum error on our interval was less than or equal to 0.01?