Solution (source code)

= Solution

Assume $f\in L^2[-L,L]$, so the squared-error <integral> is finite. Write
$$
r_N(x)=f(x)-\frac{a_0}{2}-\sum_{n=1}^Na_n\cos\frac{n\pi x}{L}.
$$
At a minimum, differentiating with respect to each coefficient gives
$$
0=\frac{\partial F}{\partial a_0}=-\int_{-L}^Lr_N(x)dx,
\qquad
0=\frac{\partial F}{\partial a_m}=-2\int_{-L}^Lr_N(x)\cos\frac{m\pi x}{L}dx.
$$
By <Fourier orthogonality>, the constant has squared norm $2L$, each nonconstant cosine has squared norm $L$, and all distinct modes are orthogonal. Thus
$$
\boxed{a_0=\frac1L\int_{-L}^Lf(x)dx,\qquad
 a_n=\frac1L\int_{-L}^Lf(x)\cos\frac{n\pi x}{L}dx\quad(1\leq n\leq N).}
$$
These are exactly the <Fourier coefficients> of the <Fourier cosine series> of the <even function>. To check that the stationary choice is a unique global minimum, perturb the coefficients by $\delta a_n$. The residual is orthogonal to every fitted mode, so the cross terms vanish and
$$
F(\boldsymbol a+\delta\boldsymbol a)-F(\boldsymbol a)
=\frac L2(\delta a_0)^2+L\sum_{n=1}^N(\delta a_n)^2\geq0,
$$
with equality only when every perturbation is zero. The truncated series is the <orthogonal projection onto a finite-dimensional subspace> spanned by the constant and the first $N$ cosine modes.