Fourier Series

We work in an interval of length

a  =  2;

We define our basis functions

f1[x_,m_] :=Sqrt[2/a] Sin[(2 Pi m)/a x];
f2[x_,m_] :=Sqrt[2/a] Cos[(2 Pi m)/a x];

Pick some function in this interval, here we let
f(x)=x

f[x_] :=x ;

We construct  both the  a and b coefficients.

acoff[n_] := 
Sqrt[2/a] Integrate[f[x]*f2[x,n],{x,-a/2,a/2}] ;

bcoff[n_] :=
Integrate[f[x]*f1[x,n],{x,-a/2,a/2}] Sqrt[2/a]

Now we construct the Fourier sum up to a maximum value
n=nmax. The Fourier series involves the sum as n->∞.

four[x_,nmax_] := 1/2 acoff[0] + 
 Sum[ acoff[n] f2[x,n],{n,1,nmax}] +
 Sum[ bcoff[n] f1[x,n],{n,1,nmax}]

nmax=3

Plot[Evaluate[{four[x,3],f[x]}],{x,-a/2,a/2}]

nmax=10

Plot[Evaluate[{four[x,10],f[x]}],{x,-a/2,a/2}]

nmax=100

Plot[Evaluate[{four[x,100],f[x]}],{x,-a/2,a/2}]