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

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

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

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