Find the function $f$ $\in$ $C[-\pi,\pi]$ such that $|| f - g ||_{2}$ is minimized? I am learning about Fourier Series and I am currently stuck on the following question: 
Let $S$ denote the set of functions in $C[-\pi,\pi]$ of the form 
\begin{align*}
f(x) = asin(x) + bsin(2x)
\end{align*}
where a and b are arbitrary real numbers. Let $g(x) = x$ for $x \in [-\pi, \pi]$. Find $f \in S$ for which $||f - g||_{2}$ is smallest.
If someone could help me understand their solution to this question and give me some direction that would be greatly appreciated. This section of Carother's Real Analysis has proven really difficult for me. Thanks in advance!  
 A: In general, if you have a vector $x$ in a Hilbert space $H$, and a (closed) subspace $M$ of $H$, then a standard theorem says that the distance between $x$ and the subspace $M$ is equal to $\|x-P_Mx\|$ where $P_M$ is the orthogonal projection onto the subspace $M$. In your problem, I would take $M$ to be the subspace $S$ of $L_2[-\pi,\pi]$ spanned by the functions of the set $S$. Since
$$\int_{-\pi}^{\pi}\sin x\sin 2x\,dx=0$$
the functions $\sin x$ and $\sin 2x$, viewed as vectors in $L_2[-\pi,\pi]$, are linearly independent (orthogonal vectors are linearly independent), hence $S$ is two-dimensional. Now the problem is to calculate the orthogonal projection of the vector $g(x)=x\in L_2[-\pi,\pi]$ onto the subspace $S$. A simple calculation shows that 
$$\int_{-\pi}^{\pi}\sin^2 x\,dx = \int_{-\pi}^{\pi}\sin 2x\,dx=\pi$$
hence the set $\{\sin x/\sqrt{\pi},\sin 2x/\sqrt{\pi}\}$ is an orthonormal basis for $S$, and so the orthogonal projection of $x$ onto $S$ is simply
$$P_Sg=\frac{1}{\sqrt{\pi}}\left(\langle g,\sin x\rangle \sin x+\langle g,\sin 2x\rangle \sin 2x\right)$$
where 
$$\langle g,\sin x\rangle=\int_{-\pi}^{\pi}x\sin x\,dx=2\pi$$
and
$$\langle g,\sin 2x\rangle=\int_{-\pi}^{\pi}x\sin 2x\,dx=-\pi$$
Thus, we obtain
$$P_Sg=\frac{2}{\sqrt{\pi}}\sin x-\frac{1}{\sqrt{\pi}}\sin 2x$$
and this is your $f\in S$ which minimises the distance.
