I want to find the Fourier cosine series for the following function:
$$f(x) = \begin{cases} x & \text{for}\enspace 0 \leq x \leq \frac{\pi}{2}\\ \pi-x & \text{for}\enspace\frac{\pi}{2}\leq x\leq \pi \end{cases}. $$
Note: Since there's so many different versions of it, here's the general Fourier series representation I'm using. If a periodic function $f$ satisfies Dirichlet conditions on $[-L,L]$ and has period $2L$, then we have:
$$f(x) \simeq \frac{a_0}{2}+\sum_{n=1}^{\infty}\left(a_n\cos\frac{n\pi x}{L}+b_n\sin\frac{n\pi x}{L}\right)$$
where
$$a_n = \frac{1}{L}\int_{-L}^{L}f(x)\cos\frac{n\pi x}{L}dx,\space n = 0, 1, 2, \cdots$$ and $$b_n =\frac{1}{L}\int_{-L}^{L}f(x)\sin\frac{n\pi x}{L}dx, \space n = 1, 2, 3, \cdots$$
To get the cosine series we delete the $b_n\sin\frac{n\pi x}{L}$ part of the sum and find a formula for $a_n$. In this case, $L = \frac{\pi}{2}$. I know $f(x)$ isn't defined on $[-L,L]$ but integrating over $[0,\pi]$ is the same. After some wrangling I have:
$$a_n = \frac{2}{\pi}\left(\int_{0}^{\frac{\pi}{2}}x\cos{2nx}\space dx + \int_{\frac{\pi}{2}}^{\pi}(\pi - x)\cos{2nx}\space dx\right)$$.
After integrating by parts I get:
$$\begin{align}a_n &= \left(\frac{2}{\pi}\cdot\frac{1}{4n^2}\right)\left((-1)^n +1+(-1)^n -1\right)\\&=\frac{1}{2n^2\pi}\left(2\cdot(-1)^n\right)\\&=\frac{(-1)^n}{n^2\pi}\end{align}$$
(I'm using the fact that $\cos{n\pi} = (-1)^n$ here)
So the Fourier cosine series for $f$ is given by:
$$f(x) \simeq \frac{a_0}{2} + \frac{1}{\pi}\sum_{n=1}^{\infty}\frac{(-1)^n}{n^2\pi}\cos{2nx}$$
However, when I try to find $a_0$ by subbing in $0$ for $n$ in $a_n$, I run into division by $0$. Is there a method of getting $a_0$ that avoids this?