Solution (source code)

= Solution

If $p$ is a minimizer, then for each $q\in\Pi_n$ the quadratic
$$
\|u-p-tq\|^2=\|u-p\|^2-2t(u-p,q)+t^2\|q\|^2
$$
has a minimum at $t=0$, so $(u-p,q)=0$. Conversely, if this orthogonality holds, then for every $q\in\Pi_n$ the <Pythagorean theorem> in the <inner product space> gives
$$
\|u-q\|^2=\|u-p\|^2+\|p-q\|^2\geq\|u-p\|^2.
$$
Thus the minimizers are exactly the <orthogonal projection>[orthogonal projections] characterized by
$$
\boxed{(u-p,q)=0\quad\text{for all }q\in\Pi_n.}
$$
For existence, write $p=\sum_{j=0}^n c_jx^j$. The conditions form the <normal equations for linear least squares>[normal equations] $Gc=b$, with <Gram matrix> $G_{ij}=(x^i,x^j)$ and $b_i=(u,x^i)$. For every nonzero coefficient vector $v$,
$$
v^TGv=\int_{-1}^1x^2\left(\sum_jv_jx^j\right)^2\,dx>0.
$$
Indeed a nonzero <polynomial> is nonzero on some open <interval> away from $0$, where the weight is positive. Hence $G$ is a <positive-definite matrix> and is invertible, proving existence and uniqueness of $c$. Uniqueness also follows from the preceding <Pythagorean theorem> identity. The zero of the weight at one point therefore causes no degeneracy on continuous functions or polynomials.