Math 166
 Computer Lab # 9
 Taylor Series

New Mathematica  Commands:   Series, Normal, Do

Goal:   To learn the commands in Mathematica to generate Taylor series and Taylor polynomials; to investigate intervals where Taylor polynomials accurately represent a function both graphically and numerically; to consider some applications of Taylor polynomials.

Introduction

We will use the two Mathematica  commands  Series and Normal to generate Taylor series and Taylor polynomials.   For example, to determine the Taylor series expansion of a function f(x) about x=x0 with 5 terms written out explicitly plus the order of the next term, we use the command Series.  If we want the Taylor polynomial of degree 5 (i.e., the truncated Taylor series) then we must ask Mathematica  to truncate the series by using the command Normal, i.e., convert it to a normal form that can be used to plot and manipulate in various ways.  The general form of the commands is given below where 
     x0 represents the point we are doing the expansion about and  
     n is the number of terms.

Series[f[x], {x,x0,n}]
 Normal[ Series[f[x], {x,x0,n}] ]

In particular, if we ask for the Taylor series for an arbitrary function f(x) about x0=0,  we have

Clear[f]
Series[ f[x], {x,0,4}]

On the other hand, if we want the Taylor polynomial of degree 4 for f(x) about x0=0, we type

Normal[ Series[ f[x], {x,0,4} ]  ]

Note that this latter result can be plotted or evaluated once we define f(x) whereas the series can not.

To get the Taylor polynomial about a point other than 0 such as a polynomial of degree 3 for f(x) about x0=1 we type

Normal[ Series[ f[x], {x,1,3} ]  ]

Example 1.  (a)  Determine the 5th degree Taylor polynomial for sin x about x=0.  (b) Determine the 5th degree Taylor polynomial for ex about x=0.  (c) Why does the polynomial for sin x only have 3 terms?

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

Recall that the expansion for sin x about x=0 does not have even powers of x (since if f(x)=sin x, then f(x), f''(x), fiv(x), etc., are ±sin x, so that when we evaluate them at x=0, we have 0).

Viewing Taylor Polynomials Graphically

We would like to use Mathematica's  animation feature to help us visualize how well the Taylor polynomial is approximating the graph of the function f(x) over some interval about x0.  What we have done  is write a function to generate a sequence of Taylor polynomials and their corresponding plots so we can animate them.  In this command we define a function called plottaylor (this is not a built-in Mathematica  command) using a Do loop from k=1 to k=n.  In this loop we first  generate a Taylor polynomial of degree k about x0 and store it in taylor[x,k]; next we plot the original function f(x) and the Taylor polynomialof degree k stored in taylor[x,k].  These steps are repeated in increments of m until k=n.

Clear[plottaylor]
plottaylor[f_,{x_,x0_,n_,m_},{a_,b_}]:=
   Block[ {taylor, k,y },
   Do[ 
     taylor[y_,k]=Normal[ Series[ f/.x->y,{y,x0,k}] ];
     Plot[ {f,taylor[x,k]},{x,a,b},
        PlotStyle->{RGBColor[1.000, 0.069, 0.116],
        RGBColor[0.177, 0.150, 1.000]}], {k,1,n,m}] ]

This command simply generates the Taylor polynomials and plots the function (in red) and each Taylor polynomial (in blue) on a separate graph for animation.  The arguments involve those for the Series command as well as the Plot command. 

In particular, the first list of arguments (after the function) {x,x0,n,m} is the same as Series except it has the added argument m.  m is the increment in the degree of polynomial; it is usually set to 1, but if you know beforehand (as in sin x or cos x) that only odd or only even powers of x occur, then you generate the polynomials in increments of 2 in the degree.  This avoids duplication of the plots.  The second set of arguments {a,b} is for the Plot command and gives the range of x-values to be plotted.

Exercise 1.  Plot f(x)=sin x on (-4,4) and the Taylor polynomials of degree 1 through 9 (only odd powers appear) which approximate sin x about x=0.  Animate. (Recall that to animate your graphs you (i) select them all, (ii) go to Graph menu, (iii) select Animate Selected Graphics from Graph menu, (iv) if the animation is too fast, select Animation from the Graph menu and set frames per second to 5 or so and click OK.)  What do you notice about the interval centered about x=0 in which the given Taylor polynomial appears to represent sin x closely?

plottaylor[Sin[x],{x,0,9,2},{ -4,4}]

In the above example we approximated sin x in the neighborhood of x=0 by Taylor polynomials and we saw that as we took higher and higher degree polynomials, their approximation seemed valid for a larger interval centered about x=0.  However, this is not the case for many functions as the following example indicates.

Example 2. Consider the function f(x) = ln(1+x).  If we do a Taylor series expansion about x=0 and look at the first 6 Taylor polynomials we see from the graph below that the Taylor polynomials become better and better approximations only in the interval (-1,1) and not for all x.  We say that the interval of convergence of the Taylor polynomial for ln(1+x) about x=0 is (-1,1).

Clear[f,p,pp]
f[x_]:=Log[1+x]
Do[ p[x_,k]= Normal[Series[f[x],{x,0,k}]], {k,1,6}]
Do[ pp[k]=Plot[ {f[x],p[x,k]},{x,-0.99,2},
        PlotStyle->{RGBColor[1.000, 0.069, 0.116],
        RGBColor[0.177, 0.150, 1.000]},
        PlotRange->{-3,3},DisplayFunction->
        Identity,Epilog->{{Text["n = ",{1,2}]},
        {Text[k, {1.4,2}] } }], {k,1,6}]
Show[GraphicsArray[ { {pp[1],pp[2]}, {pp[3],pp[4]},
{pp[5],pp[6]} } ] ]

Taylor polynomial approximation to ln(1+x) about x=0.

Looking at the error

Now we want to use Mathematica  to give more precise information about how well the Taylor polynomial approximates the function.  In particular, we want to calculate the difference in the function f(x) and its kth polynomial approximation p[x,k].  Let
          err[x,k] = | f(x) - p[x,k] | .
Notice that the error we defined is a function of x, as well as the degree of the polynomial.  Thus we can make a table of the error at a particular x value for various degrees of the polynomial.

Example 3. Consider again the function f(x) = ln(1+x) and its Taylor polynomial approximations about x=0.  Make a table of the error at x= -.9, 0, 1, 1.1, and 2 for polynomials of degree 1 through 15.  What do you conclude about the interval of convergence?  Note that the function is not defined for xæ-1.0

It is beneficial to use a Do loop for these commands.  For a Do loop we simply give several commands which we want executed for a specific range of values; in our case we want to compute the Taylor polynomial and the error for a certain range of  degrees of   polynomial.  

For each value of k we
  (i)   calculate the Taylor polynomials of degree k,
  (ii)  define the error  for the kth degree polynomial (as above),
  (iii) generate a table of values of  the degree of the polynomial and the error at each of the specified points.  We use the TableForm command to print the result in table form.

Clear[f,p,err,k]
f[x_]:=Log[1+x]

Do[
    p[x_,k]=Normal[Series[ f[x], {x,0,k}] ],{k,1,15}]
    err[x_,k_]:=N[ Abs[ f[x] - p[x,k] ] ];
    Table[ {k,err[-.9 ,k],err[.5,k],
        err[1,k],err[1.1,k],err[2,k]}, {k,1,15}];
    TableForm[%,TableHeadings->{None,{"k","x=-.9",
        "x=.5","x=1","x=1.1","x=2"}}]

The error is decreasing for x=-.9, 0.5, and 1.  For x=1.1 and x=2 the error is increasing for large k.  This suggests that the function converges for |x|<1 which is the conclusion we obtained from looking at the approximations graphically.

******************************************************************WARNING:  (1) We have NOT used a delayed equal (:=) when defining p[x,k] above because we may want to plot it later.  If we use a delayed equal and try to plot p[x,k] then x takes on a numerical value in the plot command (i.e., the points in the domain) but it is a  variable in the  Series command.  This is the same difficulty we got into trying to define  g[x_]:=D[f[x],x] and then plotting g[x] from x=1 to x=b.
          (2)  In defining p[x,k] we have NOT used k as an independent variable but rather as a storage array.  In particular we have defined it by p[x_,k] instead of p[x_,k_].
******************************************************************

Exercise 2.  We can also plot the error function and compare the plots as k increases.  Plot err[x,k] for k=1,12 (in increments of 2) from -.99<x<2  using PlotRange->{0,2} and animate the plots.  What do you conclude?

Clear[f,p,err,k]
f[x_]:=Log[1+x]
Do[
  p[x_,k]=Normal[Series[ f[x], {x,0,k}] ] ,
                                           {k,1,12}]
  err[x_,k_]:=N[ Abs[ f[x] - p[x,k] ] ];
Do[ 
   Plot[err[x,k],{x,-.99,2},PlotRange->{0,2}], 
                      {k,1,12,2}]

Math 166
Computer Lab #1
Name:_ _ _ _ _ _ _ _ _ _
Instructor:_ _ _ _ _ _ _ _ _

1.  Let f(x)=Sqrt[1+x].

(a)  Plot f(x) and the first 6 Taylor polynomials in the expansion of f(x) about x=0.  Graphically estimate the interval of convergence.

(b) Now pick a few points in your interval of convergence and just outside it (as we did in Example 3) and make a table of the degree of polynomial and the error at each point.  Do your results agree with your graphical results?

2.  Let g(x)=(1+x)7  and consider the Taylor Series (binomial series) expansion of g(x) about x=0.

(a) Make a table of the Taylor polynomials of degree æ8.
Tabulate the degree k and the polynomial.  Why are the polynomials for degree 7 and 8 the same?

(b.)  Use the command Expand[ g[x] ].  What do you conclude?

(c.)  Define the error in the Taylor approximation as in  Example 3.  Plot this error for your polynomials in (a). Why is the error zero for k=7?

(d.)  We would like to use your cubic polynomial in (a) to approximate  (i) g(0.2)  and (ii) g(2).  Find these approximations and the error in each.  Why is the approximation for g(2) not a good approximation?

(e.)  We want to approximate g(0.2) with an error æ0.001.  What is the smallest  degree polynomial that we can use to obtain an approximation valid at x=0.2 with an error less than or equal to 0.001?

3.   Recall that  e-x^2  was one of those functions for which we couldn't find an elementary antiderivative so if we wanted to evaluate the integral Üab e-x^2 dx we had to approximate it using numerical techniques such as the midpoint or Simpson's rule.  Another approach to approximating the integral is to do a series expansion for the integrand and then integrate each of the terms in the expansion.  Recall that the Taylor series expansion for e-x^2 about x=0 is valid for all x.

a.  Generate the Taylor polynomials for f(x)=e-x^2   about x=0 up to degree 12 and output the degree of the polynomial and the polynomial in table format.

b.  Approximate Ü01 e-x^2 dx by integrating each of the polynomials generated in (a).  Tabulate your results.

c.  Calculate the exact value of the integral by asking Mathematica to give a numerical approximation to the definite integral, i.e., use N[Integrate[f[x],{x,0,1}] ].  Then calculate the error in each of your approximations in (b).  Tabulate your results giving the order of the polynomial, the value of the integral of the polynomial, and the error.  (Recall that when we ask Mathematica for the antiderivative of e-x^2  we get the answer in terms of the erf function which Mathematica  can evaluate at particular values of x.)

4.  In the previous lab we investigated some formulas which gave approximations to º. Here we look at a Taylor series expansion  which leads to much more efficient computations of º than the previous ones we considered.

a. First, generate the Taylor series expansion for arctan(x) about x=0 printing out the first seven terms explicitly.

b.  Machin's formula (1706) for approximating º  can be derived using elementary trigonometry and is given by

   º/4  =  4 arctan(1/5)  - arctan (1/239).
   
Approximate  º  using this formula and  a kth degree Taylor polynomial for arctan(x) for k=1,2,...,8.  Tabulate your results.

c.  In our approximation in (b) we used the same degree Taylor polynomial to approximate arctan(1/5) and arctan(1/239).  However, from our series expansion we see that if we use a polynomial of degree (k-1) then the next term is of the order x^k.  Clearly (1/239)^k is much smaller than (1/5)^k and so we  need a higher degree polynomial to approximate (1/5)^k then (1/239)^k.  If we use a first degree Taylor polynomial to approximate arctan(1/239), what degree Taylor polynomial should we use to approximate arctan(1/5)?  To decide this you might want to try to make the errors in the computation of the two arctangents about the same.  Now use these two polynomials to obtain another approximation to  º and calculate the error.  Compare your result with (b).