Find the Fourier series $\sum_{-\infty}^{\infty} c_ne^{inx}$ of $f$, which is a $2\pi$-periodic function such that $$f(x) = |x|\quad \text{ for }\space x\in [-\pi, \pi].$$
I know how one typically goes about finding a Fourier series using the definition, but I'm unsure how to treat this. Would I take separate integrals for $[-\pi, 0]$ and $[0, \pi]$ so that we use $f(x) = -x$ in the integral from $-\pi$ to $0$ and $f(x) = x$ for the integral from $0$ to $\pi$?
Target function: $$ f(x) = |x| = \begin{cases} \phantom{-}x & x\ge 0 \\ -x & x< 0 \end{cases} $$
Since the function is even $f(x)=f(-x)$, it will be a cosine series. The Fourier amplitudes are $$ \begin{align} % a_{0} &= \frac{1}{\pi}\left( \int_{-\pi }^0 -x \, dx+\int_0^{\pi } x \, dx \right) = \pi \\ % a_{k} &= \frac{1}{\pi}\left( \int_{-\pi }^0 -x\cos(kx) \, dx+\int_0^{\pi } x \cos \left( kx \right) \, dx \right) = \frac{2 \left((-1)^k-1\right)}{\pi k^2}\\ % \end{align} $$ The first few terms are $$ \begin{array}{cr } k & a_{k} \\\hline 1 & -\frac{4}{\pi } \\ 2 & 0 \\ 3 & -\frac{4}{9 \pi } \\ 4 & 0 \\ 5 & -\frac{4}{25 \pi } \\ \end{array} $$
The convergence of the approximation is painfully slow: