You are on page 1of 5

Webpage http://hogwarts.ucsd.edu/~pkrysl/se121/Solved_exercises/html/solvedex_5_tay1.

html

Error of the Taylor series


Contents

Problem statement Solution

Link to the m-file: ../solvedex_5_tay1.m

Problem statement
Consider the function . Use an -term Taylor series at to approximate . Set successively . Plot the error as the difference between and its -term Taylor series. Is the shape of the error curve consistent with the orderof analysis of the error for the Taylor series with the various numbers of terms?

Solution
x0=0; f = sym('x-exp(-x^2)' ); x=linspace(-1, 1, 100);

With a single term Taylor series we are approximating the function with a constant. Hence the error is governed by the first neglected term, the linear term of the Taylor series.
nterms = 1; t=taylor(f, nterms, x0) plot(x,subs(f,'x',x)-subs(t,'x',x),'r-','linewidth',5-nterms) xlabel('x'),ylabel('f(x)-Taylor series of f(x)') title([' Taylor series of ' char(f) ' with ' num2str(nterms) ' terms']) t = -1

We expect the error (the difference between the original function and its Taylor series approximation) to be essentially linear in the immediate neighborhood of . With a two-terms Taylor series we are approximating the function with a linear function. Hence the error is governed by the first neglected term, the quadratic term of the Taylor series.
nterms = 2; t=taylor(f, nterms, x0) plot(x,subs(f,'x',x)-subs(t,'x',x),'r-','linewidth',5-nterms) xlabel('x'),ylabel('f(x)-Taylor series of f(x)') title([' Taylor series of ' char(f) ' with ' num2str(nterms) ' terms']) t = x - 1

We expect the error (the difference between the original function and its Taylor series approximation) to be essentially quadratic in the immediate neighborhood of . The error curve indeed resembles a parabolic arc close to . With a three-terms Taylor series we are approximating the function with a quadratic function. Hence we would expect the error to be governed by the first neglected term, the cubic term of the Taylor series.
nterms = 3; t=taylor(f, nterms, x0) plot(x,subs(f,'x',x)-subs(t,'x',x),'r-','linewidth',5-nterms) xlabel('x'),ylabel('f(x)-Taylor series of f(x)') title([' Taylor series of ' char(f) ' with ' num2str(nterms) ' terms']) t = x^2 + x - 1

We would expect the error (the difference between the original function and its Taylor series approximation) to be essentially cubic in the immediate neighborhood of . The error curve however does not resemble a cubic arc close to . It looks much more like a quadratic, or perhaps a quartic. To be on the safe side, we plot both the function and its three-term Taylor series. We see that there error is indeed of the same sign on both sides of .
plot(x,subs(f,'x',x),'r-') hold on plot(x,subs(t,'x',x),'k--') legend('f(x)','Taylor of f(x)','location','northwest') xlabel('x'),ylabel('f(x),Taylor series of f(x)') title([' Taylor series of ' char(f) ' with ' num2str(nterms) ' terms'])

The explanation will be found by taking a higher order Taylor series expansion (here we are taking five terms) and checking for the presence (or absence) of the cubic term.
t=taylor(f, 5, x0) t = - x^4/2 + x^2 + x - 1

We see that the cubic term is absent (and therefore does not contribute to the error), and hence in the figure that shows the error of the three-term Taylor series we actually see the effect of the fourth-order term of the Taylor series.

You might also like