Gerald Farin, Dianne Hansford

CPI 200 -- Math Foundations of Informatics

Demonstration of Truncation Error:

This notebook illustrates that error exists in the calculation of trigonometric functions, such as sin(x), because they are approximated by polynomials.

A Taylor series (or approximation) is a representation of a function as an infinite sum of values of its derivatives at a single point.(See the Wikipedia article for more information.)

Typically, we look at derivatives at the origin (x=0).

sin(x) ≈ x - x^3/3! + x^5/5! - x^7/7! + ....

or more compactly: 
sin(x) ≈ ((-1)^{i-1}/(2i-1)!) * x^{2i-1} for i=1,2,3,....

Below you will find a Plot of this approximation along with the "exact" sinx function. The variable imax is the max value for i above: i=1,2,...,imax

Try varying imax to see the variation in approximation quality.

The polynomial approximates best at the origin.
See the error at Pi in the printout below the plot.

Try varying the x-value range to see how well the polynomial approximates farther from the origin.

Another interesting artifact -- if you are new to plotting. Notice that when we approximate sinx by a cubic function (imax=2), that the true sinx function looks flat because of the range of y-values being plotted. See the second Plot command for use of "PlotRange" which keeps the y-range between -1 and 1 -- This makes the plot easier to understand.

Try this: Create a new cell below, and create a plot of the Taylor approximation to your favorite function. See the Wikipedia page http://en.wikipedia.org/wiki/Taylor_series for the Taylor approximation to a number of functions.

For more information, see Chapter 2 in Scientific Computing and Visualization by G. Farin and D. Hansford, AKPeters 2008.

f[x_, imax_] := Sum[((-1)^(i-1)/(2i-1)!)x^(2i-1),{i,imax}];

Manipulate[Plot[{f[x,imax], Sin[x]}, {x,-4Pi, 4Pi},PlotStyle->Thick], {{imax,2},2,15,1}]

(* imaxx is the summation range *)
imaxx= 10;
Print["Compare values at PI for imaxx=",imaxx];
Print["  Sin[Pi]=",Sin[Pi],"  Approximation=",N[f[Pi, imaxx]]]

(* Here is an example of the plot with PlotRange set *)

Manipulate[Plot[{f[x,imax], Sin[x]}, {x,-2Pi, 2Pi}, PlotRange->{-1,1},PlotStyle->Thick], {{imax,2},2,15,1}]