TITLE: If $ \int_0^{\pi}f(x)\cos(nx)\,dx=0$ for all $n$ then prove that $f\equiv 0$
QUESTION [11 upvotes]: If $f:[0,\pi]\to \mathbb R$ is continuous and $f(0)=0$ such that $\displaystyle \int_0^{\pi}f(x)\cos(nx)\,dx=0$ for all $n=0,1,2,\cdots$ then prove that $f\equiv 0$ in $[0,\pi]$.

I want to apply Weierstrass approximation theorem. As $f$ is continuous so there exists a sequence of polynomials $\{p_n(x)\}$ such that $p_n(x)\to f$ uniformly. If I expand $\cos nx=1+\frac{n^2x^2}{2!}+\cdots$ then $$\int_0^{\pi}f(x)\,dx+\frac{n^2}{2!}\int_0^{\pi}x^2f(x)\,dx+\cdots=0.$$which implies  $\displaystyle \int_0^{\pi}x^{2n}f(x)\,dx=0$ for each $n=0,1,2,\cdots$. Is this step correct ? If yes then I can deduce from it that $f\equiv 0$. If I am Not correct then solve it please.

REPLY [2 votes]: (Complete revamp of my previous answer.)
Extend $f$ by $f(x) = f(-x)$ for $x \in [-\pi,0)$, then $f$ is $2\pi$ periodic
and so for any $\epsilon>0$ there is a trigonometric polynomial $q$ such that $\|f-q\|_\infty = \sup_{|x|\le \pi} |f(x)-q(x)| < \epsilon$.
(Recall that a trigonometric polynomial is of the form
$q(t) = a_0+\sum_{k=1}^n (a_k \cos(kt) + b_k \sin (kt))$). (See, for example,
Rudin's "Real & Complex Analysis", Theorem 4.25.)
Let $\tilde{q}(t) = {1 \over 2} (q(t)+q(-t))$ and note that
$\|f-\tilde{q}\|_\infty \le {1 \over 2} (\|f-q\|+ \|f-q\|) < \epsilon$ (where 
we used evenness of $f$ for the last part). Hence we may assume that $q$ is
even to begin with, in particular, $q$ has the form
$q(t) = a_0+\sum_{k=1}^n a_k \cos(kt) = \sum_{k=0}^n a_k \cos(kt)$.
Note that, by assumption, $\int_{-\pi}^\pi f(x) q(x) dx = 0$.
Then $\int_{-\pi}^\pi f^2(x) dx = \int_{-\pi}^\pi f(x) (f(x)-q(x)+ q(x)) dx = \int_{-\pi}^\pi f(x) (f(x)-q(x)) dx$, hence
$\int_{-\pi}^\pi f^2(x) dx \le \|f\|_\infty 2 \pi \epsilon$. Since
$\epsilon>0$ was arbitrary, we see that $\int_{-\pi}^\pi f^2(x) dx = 0$. Since
$f$ is continuous, and $f^2(x) \ge 0$ for all $x$ we see that
$f(x) = 0$ for all $x$.