3. Taylor Series
Home Up Quick Reference M-files Laser Printing 1. Matlab Basics 2. Sequences & Series 3. Taylor Series 4. Fields & Flows 5. ODE Models 6. Symbolic ODE 7. Systems & 2nd order 8. Numerical ODE 9. Laplace Transform

[Objectives | Exercise | Examples ]

OBJECTIVES
Use MATLAB to graphically compare a function with its Taylor polynomial approximations.
Learn a little about MATLAB's Symbolic Toolbox (used for doing symbolic manipulations).

EXERCISE

In this problem we will consider the Taylor polynomials centered at a=0 for the function 

f(x)=sin(x^2/5) for  -4 < x 4.

 Instructions

  1. Approximate f by a Taylor polynomial Tn(x) with degree n centered at a=0. Plot the function f and Tn on the same graph over the given interval for n=4 and n=6.
  2. Use Taylor's formula to estimated the accuracy En(x) of the approximation Tn(x) to f(x) when for -4 < x 4 and n=4..

  3. Check your results in part (b) by graphing |Rn(x)|=|f(x)-Tn(x)| and the estimate En(x) you found in part b (again n=4).

To do this problem go through the two examples below. (See especially Example B below.). In this assignment you will be using MATLAB's Symbolic Toolbox. To read more about this Toolbox, see Chapter 8 of Polking and Arnold. 

Hint: You might (but  want to copy the commands used in Example B into a couple of M-Files. (Click here to learn how.)  Then edit the files to match your problems. Then run the M-Files to produce the graphs!

Examples:

Example A)

Let's use MATLAB to compare the sine function to some of its Taylor polynomial approximations. To start, we need to define a symbolic variable to use in MATLAB's Symbolic Toolbox. The following line will do just that:

>> syms x
While we're at it, let's go ahead and graph the function y=sin(x) on the intervals |x|<2pi and |y|<5.
>> a=[-2*pi 2*pi -5 5];
>> fplot('sin(x)',a,'k')
Now let's create some Taylor polynomials and add them to the plot

>> hold on    % hold on is used so we do not erase the current figure.
>> T3=taylor(sin(x),4)   % The result is      T3 =x-1/6*x^3, the third order taylor polynomial.
>> fplot(char(T3),a)
>> title('Plot of sin(x) and T_3')
>> gtext('sin(x)')
>> gtext('T_3')


 

To compute the n'th Taylor polynomial you should use the command 

T_n = taylor(f,n+1) if a=0 or T_n = taylor(f,a,n+1) otherwise.

The command gtext is useful for labeling graphs. When you type 
>>  gtext('sin(x)')
MATLAB will wait for you to point to a location on your figure where you want to insert the text string 'sin(x).' 

Repeating this process for the Taylor polynomials T5, T7, and T9 will yield the following graph:

The error term, R9(x)=sin(x)-T9(x), is given by

|R9(x)|=sin(z)x10/10! < x10/10! =:E9(x)

for some z in the interval between 0 and x. Plotting both |R9(x)| and E9(x) can be achieved using the following commands:

>> T9=taylor(sin(x),10);
>> R9=sin(x)-T9;
>> E9=x^10/gamma(11);  % gamma(n+1)=n!
>> a=[-2*pi 2*pi];
>> figure
>> fplot(char(abs(R9)),a)
>> hold on
>> fplot(char(E9),a,'r')
>> title('Plot of |R_9| and E_9, E_9 is in red')
The resulting graph is:

 

Notice that the error estimate E9(x) is above the true remainder |R9(x)| as Taylor's theorem says it should. Just out of curiousity, let's take a look at the degree 29 Taylor polynomial approximation of sin(x).

>> T29=taylor(sin(x),30);
>> figure
>> a=[-5*pi 5*pi -5 5];
>> fplot('sin(x)',a,'k')
>> hold on
>> fplot(char(T29),a,'m')

 

Example B)

In this example we will consider the function f(x)=exp(sin(x)) on the interval -2 < x 2. Here are the MATLAB commands. To simplify writing we will use the inline command to define f.

EDUğ syms x
EDUğ f=inline('exp(sin(x))')
EDUğ fc=formula(f);               % This is a "character" representation of f for use with fplot.
EDUğ T4=taylor(f(x),5)
EDUğ T6=taylor(f(x),7);
EDUğ a=[-2 2];
EDUğ fplot(fc,a,'k')
EDUğ title('Plot of f(x)=exp(sin(x), T_4 and T_6')
EDUğ hold on
EDUğ gtext('f')
EDUğ fplot(char(T4),a,'mo')
EDUğ gtext('T_4')
EDUğ fplot(char(T6),a,'gx')
EDUğ gtext('T_6')

These commands produce the plot like

To use Taylor's theorem to estimate the error between f and T_4 we will need the 5'th derivative of f. This is tedious to compute by hand. Fortunately MATLAB will do it for us. The command is  diff(f(x),n), which computes the n'th derivative of the function f(x).

EDUğ D5=diff(f(x),5)  % This gives a bit of a mess which we now plot on a new graph
EDUğ figure                % Opens a new window.
EDUğ fplot(char(D5),a)
EDUğ grid on
EDUğ title('Plot of f^{(5)}')

These commands give the following picture.

From this picture, we see that absolute value of the 5'th derivative of f is bounded by 25 on the interval of interest. Therefore by Taylor's theorem with remainder, E(x) < 25|x|^5/5!. To finish off we will graph the absolute value of R_4(x)=f(x)-T_4(x) and E(x) to verify that |R_4(x)| < E(x) as Taylor's theorem requires. Here are the commands.

EDUğ figure
EDUğ title('Plot of E and |R_4|')
EDUğ hold on
EDUğ fplot('25*abs(x)^5/gamma(6)',a,'rx')
EDUğ gtext('E')
EDUğ fplot(char(abs(f(x)-T4)),a,'k')
EDUğ gtext('|R_4|')

These produce the following plot.

Notice that the graph of E remains above |R_4| as it should.

[Objectives | Exercises | Examples |Extra Problems (Not Required) ]

Extra Problems to Try (Not Required)

 Instructions

  1. Approximate f by a Taylor polynomial Tn(x) with degree n at the number a. Plot the function f and Tn on the same graph over the given interval.
  2. Use Taylor's formula to estimated the accuracy E(x) of the approximation Tn(x) to f(x) when x lies in the given interval.
  3. Check your results in part (b) by graphing |Rn(x)| and the estimate E(x) you found in part b.

1) f(x)=(1+x)^(-1/2), a=0, n=2, 0 < x 1/2.

2) f(x)=ln(x), a=4, n=3, 3 < x < 5.  (Hint: type 'help taylor' to see how to find T_3 in this case.)

 

 

Jump to Bruce Driver's Homepage.                       Jump to Current Courses Page.

Last modified on November 18, 1999 at 04:13 PM.