(*(a) Rectangular wave H(x+pi/2)-H(x-pi/2)  *)
(*------------------------------------------*)
ClearAll["@",myN];

(*Define, but don't evaluate the Fourier coeffs*)
a[n_] := 1/Pi*Integrate[Cos[n*x],{x,-Pi/2,Pi/2}]
(*Adjust the normalization factor for a0*)
a[0] = 1/2*a[0];

(*Define the partial sum function S*)
S[myN_] := Sum[a[n]*Cos[n*x],{n,0,myN}]
(*Display S[5] for checking*)
S[5]

(*Construct a function list, and plot the approximation curves*)
flist = {Table[S[n],{n,0,5}], S[50]};
Plot[Evaluate[flist],{x,-Pi,Pi},PlotRange->All]
