Determine the fourier cosine series of the triangular wave formed by extending the function $f(x)$as a periodic function of period $2$ $$ f(x) = \begin{cases} 1+x,&-1\leq x \leq 0\\ 1-x, & \phantom{-}0\leq x \leq 1 \end{cases} $$
Here is what I have came up with but I am not sure if I am correct. Is my calculation for $a_n$ correct? Do I need to calculate $a_0$ also?
$L$ is the half period of the extended periodic function. In our case one period is given on $[-1,1]$, so the period is $2$ and $L = 1 $
I used the following half range formulas, is this correct? I only calculated one but doubled it.
$$a_n = \frac{2}{L} \int^{L}_0(1-x)\cos \left(\frac{n\pi x}{L}\right) dx= 2\int_0^1(1-x)\cos (n\pi x) dx $$
Through a series of integration I have determined and I checked it via a online calculator.
$$a_n = \frac{-2(-1)^n}{\pi ^2 n^2} + \frac{2}{\pi^2 n^2}$$
You can compress your last formula using that $$ 1-(-1)^n=\begin{cases} 2&n\text{ odd}\\ 0&n\text{ even} \end{cases} $$ thus $$ a_{2n+1}=\frac{4}{\pi^2(2n+1)^2}, $$ all other coefficients zero. Except for $a_0$, where obviously this formula does not work.
Let's see what your coefficients give, set $$ f(n,x)=4\sum_{k=0}^n\frac{\cos(\pi (2k+1)x)}{\pi^2(2k+1)^2} $$ and graph some examples with low number $n$ of terms:
which is looking good except for the missing constant term.