Solution (source code)

= Solution

Use the usual weighted $L^2$ interpretation, identifying functions equal almost everywhere; on raw pointwise functions the displayed integral is only a <seminorm>. Assume $q\in\Pi_k\subset V$. If $f-q$ is <orthogonal> to $\Pi_k$, then for every $p\in\Pi_k$,
$$
\|f-p\|^2=\|(f-q)+(q-p)\|^2=\|f-q\|^2+\|q-p\|^2\ge\|f-q\|^2.
$$
Conversely, suppose $q$ minimizes the error. For $r\in\Pi_k$ and every real $t$, compare $q$ with $q+tr$:
$$
0\le\|f-q-tr\|^2-\|f-q\|^2=-2t\langle f-q,r\rangle+t^2\|r\|^2.
$$
If the linear coefficient were nonzero, a sufficiently small $t$ with the appropriate sign would make the expression negative. Thus $\langle f-q,r\rangle=0$ for every $r$, proving \b[best approximation is equivalent to <orthogonality> of the residual].