Given the following function $$f(x)=\left\{\begin{array}{ll} 0, & 0\leq x\leq1 \\ 1, & 1<x\leq2 \end{array}\right.$$ I have to find its cosine Fourier series. I expanded it in a even way such that $$2L=4\Rightarrow L=2$$ So, $$a_{0}=\frac{2}{2}\int_{0}^{2}f(x)dx=1$$ and $$a_{n}=\frac{2}{2}\int_{0}^{2}f(x)\cos\left(\frac{n\pi x}{2}\right)dx=-\frac{2}{n\pi}\sin\left(\frac{n\pi}{2}\right)$$
I know $\displaystyle\sin\left(\frac{n\pi}{2}\right)=0$ if $n$ is even, but if $n$ is odd, it will be equal to $-1$ or $1$. My problem is: how can I rewrite this as $(-1)$ power to something? I mean, I tried $$\sin\left(\frac{n\pi}{2}\right)=(-1)^{n}$$ or even $$\sin\left(\frac{n\pi}{2}\right)=(-1)^{2n+1}$$ But I always get $1$.
The correct way to write what you want is $$\sin\left(\frac{(2m+1)\pi}{2}\right)=(-1)^m\qquad \text{and}\qquad \sin\left(\frac{2m\pi}{2}\right)=0$$ for any $m\in\mathbb Z$.