Fourier Cosine Series

Kurt Bryan and SIMIODE

A Mathematica notebook  to compute the first few terms of the Fourier cosine expansion of a function f(x) defined on an interval 0 <= x <= L.

Define the interval and the function

L = 2
f[x_] = x-x^2/3

Choose the number "n" of Fourier cosine coefficients to compute and set up an array "a" to hold coefficients

n = 5
a = Table[ (2/L)*Integrate[f[x]*Cos[k*Pi*x/L],{x,0,L}], {k, 0, n} ]

Form the Fourier cosine approximation. Note a[[k+1]] is the coefficient of cos(k*pi*x/L).

fcos = a[[1]]/2 + Sum[a[[k+1]]*Cos[k*Pi*x/L],{k,1,n}]

Plot both f(x) and the cosine approximation

plt1=Plot[f[x],{x,0,L},PlotStyle->{Red}];

plt2=Plot[fcos,{x,0,L},PlotStyle->{Blue}];

Show[plt1,plt2,AxesLabel->{"x"," "}]