Solution (source code)

= Solution

Put $x=\cos\theta$ in the <weighted inner product>. Since $T_r(\cos\theta)=\cos(r\theta)$,
$$
(T_r,T_s)=\int_0^\pi\cos(r\theta)\cos(s\theta)\,d\theta
=\begin{cases}\pi,&r=s=0,\\\pi/2,&r=s\geq1,\\0,&r\ne s.\end{cases}
$$
The last equality follows by the product-to-sum <trigonometric identity> and integration. Thus these are nonzero mutually <orthogonal polynomials>. The first $n+1$ are <linearly independent>, and the <vector space> $P_n$ has dimension $n+1$; therefore $\boxed{P_n=\operatorname{span}(T_0,\ldots,T_n)}$.

For any $q\in P_n$, the function of the real parameter $t$
$$
\|u-p_n-tq\|_2^2=\|u-p_n\|_2^2-2t(u-p_n,q)+t^2\|q\|_2^2
$$
has a minimum at $t=0$. Its <derivative> there vanishes, proving $\boxed{(u-p_n,q)=0}$. This is precisely the defining orthogonality of an <orthogonal projection>, or a <least-squares polynomial in an orthogonal-polynomial basis>. Conversely, this condition and the <Pythagorean theorem for inner product spaces> give $\|u-q\|_2^2=\|u-p_n\|_2^2+\|p_n-q\|_2^2$, proving the minimum and its uniqueness.

Expand $p_n=\sum_{r=0}^n a_rT_r$ in the <basis> already established and take the <inner product> with $T_s$. <Orthogonality> leaves $a_s(T_s,T_s)=(u,T_s)$, so
$$
\boxed{a_r=\frac{(u,T_r)}{(T_r,T_r)},\qquad p_n=\sum_{r=0}^n a_rT_r.}
$$
In particular $a_0=\pi^{-1}\int_0^\pi u(\cos\theta)\,d\theta$ and $a_r=2\pi^{-1}\int_0^\pi u(\cos\theta)\cos(r\theta)\,d\theta$ for $r\geq1$. These are the corresponding <Chebyshev series> coefficients with the constant coefficient convention used in this question.