Expand $f(x) = x$ in a cosines serie The task is:
Expand $f(x) = x$ in a cosines serie.
My approach was to use the trigonometry fourier formula
$$ c_0 + \sum_{k=1}^{\infty } a_k \cos (k \Omega t) + b_k \sin (k \Omega t) $$
set $b_k = 0$ and calculate my $a_k$
$$ a_k = \frac{2}{l} \int_0^l \cos \left( \frac{k \pi t}{l} \right) f(t) dt$$
however I  get
$$ \frac{2 t \sin \left( \frac{k \pi t}{l} \right)}{k \pi} + \frac{2 l \cos ( k \pi )}{(k \pi)^2} - \frac{2l}{k \pi} $$
The answer should be
$$ \frac{l}{2} - \frac{4 l}{ \pi ^2} \sum_{k=0}^{\infty} \frac{(-1)^{k+1} }{k} \sin (\frac{k \pi x}{l}) $$
So I believe that I'm have a totally wrong approach here. I would appreciate any help to put me in the right direction.
 A: Firstly, we need to know the interval which $f(x)=x$ is defined on. Let's assume it's defined on $(0,L)$. We also notice that $f$ is an odd function.
Since we want to approach $f$ through Fourier cosine series, we have to expand it in an even way on 
$(-L,L)$.
Thus, we define:
$g(x)=\begin{cases} -x,& -L <x<0 \\\\ x, &0<x<L \end{cases} $
Since $g(x)$ is even on $(-L,L)\implies b_k=0,\, \forall k\ge 0$.
$a_0=\displaystyle \dfrac 1 {2L}\cdot \int_{-L}^Lg(x)\,dx =\frac 1 L \cdot \int_0^L x\, dx=\frac L 2  $
$a_k\begin{array}[t]{l}=\displaystyle \dfrac 2 {2L}\cdot \int_{-L}^Lg(x)\cdot \cos\left(\frac {2k \pi x}{2L}\right)\,dx =\frac 1 L \cdot \int_{-L}^L g(x)\cdot\cos \left(\dfrac {k \pi x} {L}\right)\, dx\\\\=\displaystyle\frac 2 L \cdot \int_0^L g(x)\cdot \cos\left(\dfrac {k \pi x} {L}\right)\, dx=\frac 2 L \cdot \int_0^L x\cdot \cos\left(\dfrac {k \pi x} {L}\right)\, dx \\\\
=\displaystyle \frac{ 2\bigg(-1+(-1)^k\bigg)L}{k^2\pi^2} =\begin{cases}\dfrac{-4L}{k^2\pi^2}, & k: \text{ odd integer} \\\\ 0, & k: \text{ even integer} \end{cases} \end{array} $
So, the  Fourier cosine series of $g$ on $(-L,L)$ is:
$$a_0+\sum_{k=1}^{\infty} a_k \cdot \cos\left(\dfrac {k \pi x} {L}\right)$$
$$\dfrac L 2 - \dfrac {4L} {\pi^2}\sum_{k=1,3,5\ldots}^{\infty} \frac 1 {k^2} \cdot \cos\left(\frac {k \pi x}{L}\right) $$
If you restrict $g$ on $(0,L)$ then you have the Fourier cosine series of $f$.
