Why this formula doesn't work for $n=1$? I've been studying Fourier series and in trying to compute the Fourier series for the function $f: (-\pi,\pi)\to \mathbb{R}$ given by $f(x)=|\sin x|$ I've found something quite strange that I'm not being able to understand what I've done wrong. First, the Fourier series is
$$F_{(-\pi,\pi)}[f](x)=\dfrac{a_0}{2}+\sum_{n=1}^\infty a_n \cos nx+b_n \sin nx$$
first we see quite easily that $b_n = 0$ for all $n\in \mathbb{N}$ while $a_0 = 4/\pi$. On the other hand we have
$$a_n = \dfrac{1}{\pi} \int_{-\pi}^\pi f(x) \cos nx dx = \dfrac{2}{\pi}\int_0^\pi \sin x \cos nx dx$$
then I've used the fact that $\sin x = \frac{1}{2i}(e^{ix}-e^{-ix})$ and $\cos nx = \frac{1}{2}(e^{inx}+e^{-inx})$ to find out that
$$\sin x \cos nx = \dfrac{1}{4i}(e^{ix}-e^{-ix})(e^{inx}+e^{-inx})=\dfrac{1}{2}\left[\sin((n+1)x)-\sin((n-1)x)\right]$$
so that substituting this on the formula for $a_n$ gives
$$a_n = \dfrac{-2}{\pi(n^2-1)}[(-1)^n+1]$$
this certainly doesn't work for $n=1$ because we get a zero on the denominator. On the other hand, using the formula for $a_n$ with $n=1$ gives
$$a_1 = \dfrac{2}{\pi}\int_0^\pi \sin x \cos x dx = 0.$$
Why the calculation I did doesn't work for $n=1$? I can't find what I've done wrong. Is there something I've missed?
 A: It does work in a (very poor) sense, because $(-1)^n = -1$ for $n=1$, so in
$$
a_n = \frac{-2}{\pi(n^2-1)}[(-1)^n+1]
$$
there are terms in the numerator and denominator that are zero. So if you prioritize the $(-1)^n+1 = 0$ part then we could interpret $a_1$ to be $0$. But it's more rigorous to say that $a_1$ is ill-defined (since $0/0$ is ill-defined), and derive a separate formula. (And the reasoning with competing denominators is bound to lead you to errors eventually; it works coincidentally here.)
To see more clearly why this is necessary, note that your formula for $\sin x\cos nx$ for $n=1$ gives
$$
\sin x\cos x = \frac{1}{2}\sin 2x
$$
since $\sin(n-1)x = \sin 0 = 0$. So you can't expect to use the formula for the integral of sine:
$$
\int_{0}^\pi \sin((n-1)x)~dx = -\frac{1}{n-1}\cos((n-1)x)\bigg|_{0}^\pi = -\frac{1}{n-1}[(-1)^{n-1} - 1],
$$
but the equality between the first to the second quantity only holds if $-\frac{1}{n-1}\cos((n-1)x)$ is a bona fide antiderivative of $\sin((n-1)x)$. But for $n=1$, $\sin((n-1)x) = \sin 0 = 0$ and $-\frac{1}{n-1}\cos((n-1)x) = -\frac{1}{0}$, which hardly makes for a convincing antiderivative of $0$. Therefore this case must be handled separately.
