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

Fourier Cosine Series

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

l=2; (* The domain *) 
nn= 20; (* Partial Sum *) 
f[x_] =x ; (* Function Definition *) 
a[n_]:=Integrate[f[x]Cos[n Pi x/l], {x, 0, l}]; 
coeffs = Table[a[n], {n, 0, nn}]; 
S[n_, x_]:=coeffs[[1]]/l+Sum[2 coeffs[[i+1]]/l Cos[i Pi x/l], {i, 1, n}]; 
graph1=Plot[f[x], {x, 0, l}]; 
snapshots[i_]:=Plot[S[i,x], {x, 0, 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