This notebook creates a cosine - series representation of a couple of functions.
To go through it, click on each line that has code and press Ctrl+Enter to execute the code.

We will work with the orthonormal basis on C[0,1] using cosines:

g[n_,x_]:=If[n==0,1,(Sqrt[2])Cos[π n x]]

We can compute Fourier coefficients of a function f by integrating it against each g[n,x]. Ignore the “quiet” and “chop” commands; they just make things run more smoothly.

GetFourierCoefficients[f_]:=Quiet[Chop[NIntegrate[f[x]g[#,x],{x,0,1}]&/@Range[0,100]]]

We can now pick a function and approximate it by a cosine series:

We define the function f  as follows:

f[x_]:=Abs[x-.5]-(((x-.5))^2)

Here’s what it looks like:

Plot[f[x],{x,0,1}]

Let’s store the Fourier coefficients as the variable a:

a=GetFourierCoefficients[f]

Notice that the norm-squared of f is very close to the sum of squares of the coefficients:

MyNormSquared[f_]:=NIntegrate[f[x]f[x],{x,0,1}]

MyNormSquared[f]

Total[((#)^2)&/@a]

We can now plot f and its approximations using the first n cosines. Use the slider to increase n.

Manipulate[
Plot[{Total[a[[#+1]]g[#,x]&/@Range[0,n]],f[x]},{x,0,1}],
{n,0,100,1}]

Now let’s try something more applied, say France’s GDP. We can grab the GDP of France between 1970 and 2005 as follows:

FranceGDP=CountryData["France",{{"GDP"},{1970,2005}}];

It looks as follows:

DateListPlot[FranceGDP]

As is, FranceGDP is a function on [1970,2005] whose outputs will be $/year. We can turn it into a function on [0,1] as follows:

FranceGDPOn01[x_]:=QuantityMagnitude[CountryData["France",{{"GDP"},{1970,2005}}][(1-x)AbsoluteTime[{1970}]+x AbsoluteTime[{2005}]]]

For some reason the Plot command doesn’t work on FranceGDPOn01, so don’t use it.

In any case, we can still use it in the Fourier transform (though it takes a bit longer to compute):

b=GetFourierCoefficients[FranceGDPOn01]

And we can plot the approximations (note that we’re not plotting FranceGDPOn01 since it’s not working for some reason):

Manipulate[
Plot[{Total[b[[#+1]]g[#,x]&/@Range[0,n]]},{x,0,1}],
{n,0,100,1}]