Solution (source code)

= Solution

Write $p_n(x)=\sum_{j=0}^nc_jx^j$. The allowed <orthogonal projection> characterization, applied to the <basis> $1,x,\ldots,x^n$, gives the <normal equations>
$$
\boxed{\sum_{j=0}^nG_{ij}c_j=d_i\quad(0\leq i\leq n),\qquad G_{ij}=\int_{-1}^1x^{i+j}\,dx,\quad d_i=\int_{-1}^1u(x)x^i\,dx.}
$$
Explicitly $G_{ij}=0$ if $i+j$ is odd and $G_{ij}=2/(i+j+1)$ if it is even. These equations can also be derived by differentiating $\int(u-\sum c_jx^j)^2dx$ with respect to each coefficient.

The <Gram matrix> $G$ is a <symmetric positive-definite matrix>. For any nonzero real coefficient <vector> $z$,
$$
z^TGz=\int_{-1}^1\left(\sum_{j=0}^nz_jx^j\right)^2dx>0.
$$
The <polynomial> inside the square is nonzero, so its square is positive on some interval. Hence $G$ has zero <kernel> and is invertible. Thus \b[the normal equations have exactly one solution], giving exactly one best <least-squares polynomial>. This proves both existence and uniqueness. For completeness, if the residual is <orthogonal> to $\Pi_n$, then for every $q\in\Pi_n$ the <Pythagorean theorem> gives $\|u-q\|^2=\|u-p_n\|^2+\|p_n-q\|^2$, directly verifying minimality and uniqueness.