(* Fourier expansion of |cosx| *)

ClearAll["@"]

(* Define the Fourier coefficients *)
a[n_] := 2/Pi*Integrate[Cos[x]Cos[2n*x], {x, -Pi/2, Pi/2}]
a[0] = a[0]/2

(* Simplify the general coefficient for checking manual result *)
FullSimplify[a[n]] /. {Cos[n*Pi] -> (-1)^n, Sin[n*Pi] -> 0}

(* Assign and display the partial sum S5 *)
S5 = Sum[a[n]*Cos[2n*x], {n, 0, 5}]

(* Form a list of functions and plot *)
flist={Abs[Cos[x]], S5};
Plot[Evaluate[flist], {x, -Pi, Pi}]
