Taylor and Fourier Series Approximations

Adam Rumpf, 11/6/2017

Introduction

This demonstration defines two Manipulate environments to explore the construction of Taylor series or Fourier series approximations. Both include a dropdown menu to select an example function and a slider to select a number of terms. The display will show a plot of the function along with its approximation of the specified degree. The Tayor series environment also includes a slider to select the center. The Fourier approximation has no specified center, and instead attempts to approximate the function restricted to the interval [-2,2].

The purpose of this demonstration is to illustrate the effects of adding more terms to the approximation, and how in general this causes the approximation to be closer to the original function over a larger area. Unusual features, like discontinuity, may cause problems for the Taylor approximation, but less of a problem for the Fourier approximation. The Taylor approximation is generally best near its center.

Code

Initialization

(* explicit definition of Taylor series for function f, n terms, center c, and variable x *)
taylor[f_,n_,c_,x_]:=Sum[(((D[f[x],{x,i}]/.x->c))/(i!))(((x-c))^i),{i,0,n}]

(* explicit definition of Fourier series for function f, n terms, and variable x *)
fouriercoeff[f_,0]:=(1/4)NIntegrate[f[x],{x,-2,2}];
fouriercoeff[f_,n_]:=(1/4)NIntegrate[f[x] (E^(I Pi n x/2)),{x,-2,2}];
fourier[f_,n_,x_]:=Sum[fouriercoeff[f,m](E^(-I Pi m x/2)),{m,-n,n}];

Demonstration

(* Taylor Series *)
Manipulate[Show[Plot[f[x],{x,-3Pi,3Pi},PlotRange->{{-3Pi,3Pi},{-3Pi,3Pi}},AspectRatio->Automatic,PlotStyle->Blue,Exclusions->-1],Plot[Evaluate[taylor[f,n,c,x]],{x,-3Pi,3Pi},PlotRange->{{-3Pi,3Pi},{-3Pi,3Pi}},AspectRatio->Automatic,PlotStyle->Red],Graphics[{Black,PointSize[Large],Point[{c,f[c]}]}]],{{f,Sin},{Sin->"sin(x)",Cos->"cos(x)",Exp->"(E^x)",Function[x,Log[x+1]]->"ln(x+1)",Function[x,5(E^(-(x^2)/5))]->"5(E^(-(x^2)/5))",Function[x,0.0025(x+10)(x+1)(x-2)(x-7)]->"0.35 +0.16x-0.1875(x^2)+0.005(x^3)+0.0025(x^4)",Function[x,0.01x(x+5)(x-6)+(5/((((x+2))^2)+1))]->"-0.3x-0.01(x^2)+0.01(x^3)+(5/((((x + 2))^2) + 1))",Function[x,(1/(x+1))]->"(1/(x + 1))"}},{{c,0},-9.4,9.4,0.1},{n,0,20,1}]

(* Fourier Series *)
Manipulate[Quiet[Show[Plot[{f[x],Evaluate[fourier[f,n,x]]},{x,-4,4},PlotRange->{{-4,4},{-4,4}},AspectRatio->Automatic,PlotStyle->{Blue,Red}]]],{{f,Identity},{Identity->"x",Function[x,3(E^(-3(x^2)))]->"3(E^(-3 (x^2)))",ArcTan->"(tan^(-1))(x)",Function[x,SquareWave[x/4]+1]->"Square Wave",Function[x,2UnitStep[x]]->"Unit Step",Function[x,TriangleWave[x/4]+1]->"Triangle Wave",Function[x,2SawtoothWave[x/2]]->"Sawtooth Wave",Function[x,Piecewise[{{(3/4)(x^2),-2≤x<0},{(1/2)x,0≤x≤2}},0]]->"Piecewise Function"}},{{n,1},0,10,1}]