Let $f$ be continuous with $f(0) = f(\pi) = 0$, and $a_n = \frac{2}{\pi} \int_0^\pi \sin(nx)f(x) dx$. Prove that $\sum_{n=1}^\infty a_n^2$ converges 
Let $f$ be a continuous function with $f(0) = f(\pi) = 0$, and let $a_n = \frac{2}{\pi} \int_0^\pi \sin(nx)f(x) dx$. Prove that $\sum_{n=1}^\infty a_n^2$ converges.

For now I tried:
$\sum_{n=1}^\infty a_n^2 \leq \sum_{n=1}^\infty \frac{4}{\pi^2} \int_0^\pi \sin^2(nx)[f(x)]^2 dx$ (as $\left(\int_a^b g(x) dx)\right)^2 \leq \int_a^b (g(x))^2 dx$), but then I don't know how to proceed (or if I'm on the right track). Can I get some hints?
 A: This follows from the general Bessel's inequality in functional analysis. In this elementary context, we can simply establish the inequality as follows, for any $m\ge 1$, we have
$$\begin{aligned} 0 &\le \int_0^\pi (f(x)-\sum_{n=1}^m a_n\sin(nx))^2dx \\&=\int_0^\pi f(x)^2 -2\sum_{n=1}^ma_n\int_0^\pi f(x)\sin(nx)+\sum_{n, n'=1}^ma_na_{n'}\int_0^\pi\sin(nx)\sin(n'x)\\ &=\int_0^\pi f(x)^2-\pi\sum_{n=1}^m a_n^2 +\frac{\pi}{2}\sum_{n=1}^ma_n^2\end{aligned}$$
where we have used the elementary fact $\int_0^\pi \sin(nx)\sin(n'x) = \begin{cases} 0 & n\not=n' \\ \frac{\pi}{2} &n=n' \end{cases}$
Hence $$\sum_{n=1}^ma_n^2\le \frac{2}{\pi}\int_0^\pi f(x)^2$$
And the upper bound is essentially tight.
The above concrete calculation might be a bit misleading, in the sense that we are really just expanding $f(x)$ considered as a vector through the orthogonal (but not normal) family $\sin(x), \sin(2x)$, etc with respect to the inner product $\langle f, g\rangle :=\int_0^\pi f(x)g(x)dx$, which can be made completely abstract and more enlightening.
