Fourier Series Example

Definition of the function

f[x_]:=x;

Computation of the Fourier components

a0:

1/π*Integrate[f[x],{x,-π,π}]

an:

1/π*Integrate[Cos[n*x]*f[x],{x,-π,π},Assumptions->n∈Integers]

bn:

Simplify[1/π*Integrate[Sin[n*x]*f[x],{x,-π,π},Assumptions->n∈Integers],Assumptions->n∈Integers]

g[x_,M_]:=Sum[-((2 (((-1))^n))/n)*Sin[n*x],{n,1,M}]

Plot[{f[x],g[x,5],g[x,10],g[x,15],g[x,20]},{x,-π,π},PlotStyle->Thick,AxesLabel->{"x","f(x)"},LabelStyle->Directive[Black, Large],PlotRange->{-5,4}]