Question: Suppose $f(x)= a_0 + \sum_{k=1}^N a_k\cos(kx)$
Show that $$ \int_0^\pi f^{2}(x) = \pi a_0^2 + \frac{\pi}{2} \sum_{k=1}^N a_k^2 $$
So far I have:
$$ \begin{equation}\int_0^\pi f^{2}(x)=\int_0^\pi a_0^2 \,dx + 2a_0 \int_0^\pi \sum_{k=1}^N a_k\cos(kx) \,dx + \int_0^\pi \left(\sum_{k=1}^N a_k\cos(kx)\right)^2 \,dx \tag{$\ast$}\end{equation}$$
The first term in $(\ast)$ gives the term $\pi a_0^2$ and using term by term integration. For the second term in $(\ast)$, we get a series of $\sin(kx)$ with coefficients $\frac{a_k}{k} $ being evaluated at $0$ and $\pi$ hence the second term goes to $0$.
I am having trouble with caculating the third term which is $\int_0^\pi \left(\sum_{k=1}^N a_k\cos(kx)\right)^2 \,dx$.
Is my expansion of $f^2(x)$ correct and is splitting up the integral the best method?
My biggest question is how to evaluate the third term in $(\ast)$? If anyone could give me some guidance on how to approach this, it would be greatly appreciated.
If $k,j\in\mathbb{N}$ are different integers we have $\int_{0}^{\pi}\cos(kx)\cos(jx)\,dx = 0$, so
$$\begin{eqnarray*} \int_{0}^{\pi}f(x)^2\,dx &=& \int_{0}^{\pi}\left(a_0+\sum_{k\geq 1}a_k \cos(kx)\right)\left(a_0+\sum_{k\geq 1}a_k \cos(kx)\right)\,dx\\&=&\int_{0}^{\pi}a_0^2+\sum_{k\geq 1}a_k^2\cos^2(kx)\,dx\\&=&\pi a_0^2+\frac{\pi}{2}\sum_{k\geq 1}a_k^2. \end{eqnarray*}$$