Find a function to minimize norm. I have a problem I cannot find a solution to by myself. It goes like this:
We have a Hilbert space spanned by the family of functions $\{\sin(x), \cos(x), \sin^2(x), \cos^2(x), \sin(2x)\}$. The scalar product of this space is defined as $$\langle f,g\rangle =\int_0^{2 \pi} f(x) \cdot g(x)dx$$ 
Now I have a function $f$ that is not in the space mentioned above. I need to find a function $f_0$ from the space above, so that the norm $||f-f_0||$ becomes minimal. 
I tried it many times with the Cauchy-Schwarz relation and tried to make the first and second derivative to find the minimum, but I did not get any good result. Can someone help? I don't think the problem is very hard, but I may be on the wrong way.
Thank you in advance!
 A: Suppose that $f$ is a square integrable function on $[0,2\pi]$, i.e. $f\in  L^2[0,2\pi]$, your problem is amount to finding the orthogonal projection of $f$ into a subspace of $H\subset L^2[0,2\pi]$ spanned by $\{\sin(x), \cos(x), \sin^2(x), \cos^2(x), \sin(2x)\}$. 
First, observe that $$\sin^2(x)=1-\cos^2(x)$$ so we can replace $\ \sin^2(x)$ with $1$ (why?). Further more, $$\cos^2(x)=\frac 12 (1+\cos(2x))$$ so we replace $\cos^2(x)$ with $\cos(2x)$ (why?). 
Therefore 
$$H=\text{span}\{\sin(x), \cos(x), 1, \cos(2x), \sin(2x)\}=\text{span}\{1,\sin(x), \cos(x),\sin(2x),\cos(2x)\}
$$
The new family on the i.e.$\{1,\sin(x), \cos(x),\sin(2x),\cos(2x)\}$ is a lot easier to deal with since it's an orthogonal family (verify this!), so $$f_0=c_0+c_1\sin(x)+c_2\cos(x)+c_3\sin(2x)+c_4\cos(2x)$$
for some real number (or complex, if you'd like) $c_0,...,c_4$. To optimize the value of $c_0,...,c_4$ so that $||f-f_0||$ is minimized, we use the fact that 

$f_0$ is the best approximation of $f$ in $H$ (in the sense that $||f-f_0||$ is minimized)
  if $$\langle f-f_0,g\rangle =0$$ for all $g\in H$.

This fact can be found in every book on functional analysis in case you've never heard of it. It can be formulated in a way that is more useful to use by

$$\langle f,g\rangle =\langle f_0,g\rangle \ \ \forall g\in H$$

By denoting $g_0:=1, g_1:=\sin(x),..., g_4:=\cos(2x)$, we can see that $g_i\in H$  for all i=0,...,4 so we have
$$\begin{align}
\langle f,g_i \rangle &= \langle f_0,g_i \rangle \\
&= \langle c_0+c_1g_1+c_2g_2+c_3g_3+c_4g_4,g_i \rangle \\
&= c_0\langle 1,g_i \rangle +c_1\langle g_1,g_i \rangle+c_2\langle g_2,g_i \rangle+c_3\langle g_3,g_i \rangle+c_4\langle g_4,g_i \rangle \\
&= c_i\langle g_i,g_i \rangle
\end{align}$$
since the orthogonality of $g_n$ dictates that $\langle g_j,g_i \rangle=0$ if $i\ne j$, therefore we have
$$
c_i=\frac {\langle f,g_i \rangle}{\langle g_i,g_i \rangle}
$$
Direct calculation show that $\langle 1,1 \rangle=2\pi$ and that $\langle g_i,g_i \rangle=\pi$ for $i=1,2,3,4$. Thus 

$$\begin{align}
f_0&=c_0+c_1g_1+c_2g_2+c_3g_3+c_4g_4 \\ 
&= \frac {\langle f,1 \rangle}{2\pi}+\frac{\langle f,\sin(x)\rangle}{\pi}\sin(x)+
\frac{\langle f,\cos(x)\rangle}{\pi}\cos(x) +\frac{\langle f,\sin(2x)\rangle}{\pi}\sin(2x)+
\frac{\langle f,\cos(2x)\rangle}{\pi}\cos(2x)
\end{align}$$

is the best approximation of $f$ in $H$.
