You may have to do this the old fashion way. Generally if there is more than one form of a correct answer, you probably will not get the exact one you are looking for without some manipulation.
Clear["Global`*"]
f[x_] = Cos[x]*(UnitStep[x + Pi/2] - UnitStep[x - Pi/2])
Your Piecewise works also
$Assumptions = n \[Element] Integers
Set f[x]
to be the form
eqn1 = f[x] == an Cos[n x] + bn Sin[n x]
to get an
Simplify[Integrate[eqn1[[1]]*Cos[n*x], {x, -Pi, Pi}] == Integrate[eqn1[[2]]*Cos[n*x], {x, -Pi, Pi}]];
an = an /. Solve[%, an][[1]] // FullSimplify;
(*(2*Cos[(Pi*n)/2])/(Pi - Pi*n^2)*)
Except for Pi
in the denominator, these are the same coefficients that bills computed in his answer.
For n = 0
, a0
is 1/2 the general n
coefficient.
a0 = 1/2 an /. n -> 0
(*1/Pi*)
For n = 1
, the denominator of an
is 0, so take the limit.
a1 = Limit[an, n -> 1]
(* 1/2 *)
solve for bn
Simplify[Integrate[eqn1[[1]]*Sin[n*x], {x, -Pi, Pi}] == Integrate[eqn1[[2]]*Sin[n*x],
{x, -Pi, Pi}]]
(*bn==0*)
bn = 0;
The Sin terms had better go away for an even function.
Look at the first few terms of the series for n > 1
.
Table[an Cos[n x], {n, 2, 10}]
(*{(2*Cos[2*x])/(3*Pi), 0, -((2*Cos[4*x])/(15*Pi)), 0,(2*Cos[6*x])/(35*Pi)}*)
Terms for odd values of n
are 0, so simplify by changing n
to 2n
and starting the series at n = 1
.
an = an /. n -> 2 n // Simplify
(*(2*(-1)^n)/(Pi - 4*Pi*n^2)*)
and your Fourier series becomes:
series = a0 + a1*Cos[x] + Sum[an*Cos[2*n*x], {n, 1, Infinity}]
(*Sum[(2*(-1)^n*Cos[2*n*x])/(Pi - 4*Pi*n^2), {n, 1, Infinity}] + Cos[x]/2 + 1/Pi*)
which looks like the answer you are looking for.