Solution (source code)

= Solution

The optimal approximation is the <orthogonal projection> onto the <linear span> of $p_0,\ldots,p_N$. Their <orthogonality> gives
$$
\left\|f-\sum_{n=0}^N a_np_n\right\|_w^2
=\|f\|_w^2-2\sum_{n=0}^N a_n\langle f,p_n\rangle_w
+\sum_{n=0}^N a_n^2\|p_n\|_w^2.
$$
Completing each square proves both optimality and uniqueness:
$$
\boxed{a_n=\frac{\langle f,p_n\rangle_w}{\|p_n\|_w^2}}.
$$
For real $f$, these are real coefficients. If complex-valued $f$ is allowed while the coefficients remain real, replace the numerator by its real part and use the modulus-squared norm. The question's real formulation corresponds to real $f$.

Here the first three squared norms are one. Using $\int_0^\infty x^j e^{-2x}\,dx=j!/2^{j+1}$,
$$
\begin{aligned}
a_0&=\int_0^\infty e^{-2x}(1+x)\,dx=\frac12+\frac14=\frac34,\\
a_1&=\int_0^\infty e^{-2x}(1-x^2)\,dx=\frac12-\frac14=\frac14,\\
a_2&=\int_0^\infty e^{-2x}\left(1-x-\frac32x^2+\frac12x^3\right)dx
=\frac12-\frac14-\frac38+\frac3{16}=\frac1{16}.
\end{aligned}
$$
Thus
$$
\boxed{(a_0,a_1,a_2)=\left(\frac34,\frac14,\frac1{16}\right),\qquad
f_2(x)=\frac{17}{16}-\frac38x+\frac1{32}x^2}.
$$