How to prove that there exists real polynomials $g(x), h(x)$ such that $f(x)=g^2(x)+h^2(x)$? Prove that a real polynomial $f(x)\geqslant 0, \forall x$ if and only if there exists two real polynomials $g(x), h(x)$ such that $f(x)=g^2(x)+h^2(x)$?

The sufficiency is trivial. 
But my question is how to deal with the necessity. 

Here is another problem that I've solved.
Suppose that real polynomial $f(x)=a_nx^n+\cdots+a_1x+a_0$ with $a_n\neq 0$ has no real root, then there exists two real polynomials $g(x)$ and $h(x)$ such that 
$f(x)=g^2(x)+h^2(x)$.

 A: Hint: Factor out the real roots of $f$ to reduce to the case you've already solved.
A full solution is hidden below.

 Every real root of $f$ must have even multiplicity (otherwise $f$ would change sign at the root).  So we can write $f=a^2b$ for real polynomials $a$ and $b$ where $b$ has no real roots (here $a^2$ is just all the factors from the real roots of $f$).  Then by the problem you say you have already solved, you can write $b=g^2+h^2$, and thus get $$f=(ag)^2+(ah)^2,$$ as desired.

 Just for completeness, here's a proof of the case when $f$ has no real roots.  In that case, all the complex roots of $f$ come in conjugate pairs, so we can factor $$f(x)=c(x-z_1)(x-\overline{z_1})\dots(x-z_n)(x-\overline{z_n})$$ for $c>0$ and non-real complex numbers $z_1,\dots,z_n$.   Let $p(x)=\sqrt{c}(x-z_1)\dots(x-z_n)$, so $f=p\overline{p}$.  Writing $p=g+ih$ where $g$ and $h$ have real coefficients, we then get $f=(g+ih)(g-ih)=g^2+h^2$.

A: Alternative proof:
Let $f_n(x) = f(x) + {1 \over n}$. Then $f_n(x) >0$ for all $n,x$.
Hence there are $g_n,h_n$ such that $f(x) +{ 1\over n}= g_n^2(x)+h_n^2(x)$ for all $n,x$. The degrees of $g_n,h_n$ are bounded by the degree of $f$ hence there is
some subsequence such that $g_{n_k} \to g$ and $h_{n_k} \to h$ using, say,  the $C[0,1]$
norm. Then $g,h$ are polynomials and $f(x) = g^2(x)+h^2(x)$ for all $x$.
