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]; 
     llabel=StringJoin["The function and its ",ii,"-th partial sum"]; 
     Show[graph1,snapshots[i], PlotLabel->llabel, 
     DisplayFunction->$DisplayFunction, PlotRange->All], 
{i, 5, nn, 5}] 

go to Top