So I have this piecewise function
\begin{align} f(x) = 2, 0\leq x < \pi /2 \\ f(x) = -1, \pi/2\leq x < \pi \end{align}
I want to expand this to a Fourier cosine series. Now I have found the coefficients
$a_0= \frac12$, and $a_n = \frac6\pi \frac 1n \sin(n\frac\pi2) \\$ by calculating the integrals. Now one issue I have is that I want to rewrite this nicely in the series. The series will be by my method
\begin{equation} \frac12 + \frac 6\pi \sum_{n=0}^\infty \frac1n\sin(\frac{\pi n} 2)cos(nx) \end{equation}
but the book is showing this:
\begin{equation} \frac12 + \frac 6\pi \sum_{n=0}^\infty \frac{(-1)^n}{(2n+1)}(cos2n+1)x \end{equation}
I really do not understand if I have done something wrong, but I really wonder how I transform this ?
Let's consider your series.
Note that if n is even, sin($\frac{n\pi}{2}$)=0. So we only need to consider the odd terms, just like the series in the book ("$2n+1$").
Also, if it is a number of the type $4k+1$, $sin(\frac{n\pi}{2}$)=1, if it's a number of the type $4k+3$, $sin(\frac{n\pi}{2}$)=-1. Therefore we can rewrite your series as the one shown in the book.