Skip to main content Skip to footer site map
Reza Malek-Madani

Fourier Series

Here is how one finds the Fourier series of the function f(x) = x^2 - x, with x in (-2, 2)and plots its partial sums.

l=2; (* The domain *) 
nn= 20; (* Partial Sum *) 
f[x_] =x^2-x ; (* Function Definition *) 
a[n_]:=Integrate[f[x]Cos[n Pi x/l], {x, -l, l}]; 
b[n_]:=Integrate[f[x]Sin[n Pi x/l], {x, -l, l}]; 
coeffs1 = Table[a[n], {n, 0, nn}]; 
coeffs2 = Table[b[n], {n, 1, nn}]; 
S[n_, x_]:=coeffs1[[1]]/(2 l)+Sum[coeffs1[[i+1]]/l Cos[i Pi x/l]+ 
coeffs2[[i]]/l Sin[i Pi x/l], {i, 1, n}]; 
graph1=Plot[f[x], {x, -l, l}]; 
snapshots[i_]:=Plot[S[i,x], {x, -l, l}, DisplayFunction->Identity]; 
Do[ii=ToString[i]; 
     llabel=StringJoin["The function and its ",ii,"-th partial sum"]; 
     Show[graph1,snapshots[i], PlotLabel->llabel, 
     DisplayFunction->$DisplayFunction, PlotRange->All], 
{i, 5, nn, 5}] 
[Graphics:fourier3gr2.gif][Graphics:fourier3gr1.gif][Graphics:fourier3gr2.gif][Graphics:fourier3gr3.gif][Graphics:fourier3gr2.gif][Graphics:fourier3gr4.gif][Graphics:fourier3gr2.gif][Graphics:fourier3gr5.gif][Graphics:fourier3gr2.gif][Graphics:fourier3gr6.gif]

go to Top