Mathematical Principles for Scientific Computing and Visualization  
http://www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford


This notebook demonstrates Taylor approximations.
Taylor's Theorm states that a function f can be approximated by a polynomial.
Here we approximate sin(x) with a degree 10 and a degree 20 polynomial.

We compare the solution using 10 steps and 30 steps.
Compare solution with the solution using Huen's method in Huen.nb.

See Chapter 8: Data Fitting for details.

Updated May 2008

(* Set output folder to folder this files lives in *)

SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];

(* Load 20  sine Taylor series values and print out -- just to see values *)

N[Normal[Series[Sin[x],{x,0,20}]]]

(* Create the Taylor approximation to Sine. The approximation degree is the variable imax *)
(* imax=1 is degree 1, imax=2 is degree 3, etc *)

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

(* Create a manipulate plot to see the different approximations *)

Manipulate[Plot[{taylorSine[x,imax], Sin[x]}, {x,-4*Pi, 4*Pi},PlotStyle->Thick, PlotLabel->{"Taylor Approximation of degree ",(2*imax-1)}], {{imax,2},2,15,1}]

(* Create the degree 9 approximation to sine at x=0 
pol10[x_]:=2.7557319223985893`*^-6 (x^9)-0.0001984126984126984` (x^7)+0.008333333333333333` (x^5)-0.16666666666666666` (x^3)+x;
*)

(* Create the degree 19 approximation to sine at x=0 
pol20[x_]:=-8.22063524662433`*^-18 (x^19)+2.8114572543455206`*^-15 (x^17)-7.647163731819816`*^-13 (x^15)+1.6059043836821613`*^-10 (x^13)-2.505210838544172`*^-8 (x^11)+2.7557319223985893`*^-6 (x^9)-0.0001984126984126984` (x^7)+0.008333333333333333` (x^5)-0.16666666666666666` (x^3)+x;
*)


(* Plot Mathematica's Sine Taylor approximation *)
splot=Plot[Sin[x],{x,0,15},PlotStyle->{Thickness[0.015], GrayLevel[0.0]},BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11}, ColorOutput->CMYKColor]

(* Plot the degree 9 approximation  -- imax = 5 *) 
(* Since it starts to go off at x=5, let's stop plotting it there *)
pplot10=Plot[taylorSine[x, 5],{x,0,5.2},PlotStyle->{Thickness[0.015],GrayLevel[0.7]},BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11},ColorOutput->CMYKColor]

(* Plot the degree 19 approximation -- imax=10 *) 
(* Since it starts to go off at x=8.7, let's stop plotting it there *)
pplot20=Plot[taylorSine[x, 10],{x,0,8.7},PlotStyle->{Thickness[0.015],GrayLevel[0.5]},BaseStyle->{"FontFamily"->"Helvetica","FontSize"->11},ColorOutput->CMYKColor]


(* Create a combined graphics to postscript file *)
pic=Show[splot, pplot10,pplot20, splot]

(* Output combined graphics to postscript file *)
Export["Taylor.eps",pic];