<s>[INST] Evaluating the integral $\int_{-\infty}^\infty x^2\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2} \ dx$ 
How does one evaluate $$\int_{-\infty}^\infty x^2\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2} \ dx ?$$

The result is $1$ and it corresponds to $E[X^2]$, where $X$ is a random variable with $X\sim\mathcal{N}(0,1)$. I have tried to do some substituions and I've tried integration by parts but didn't succeed to integrate it. With the integration by parts I ended up with a harder integral in both cases and I couldn't find a good substitution.
 [/INST] Here are two solutions that exploit the same two tools in different orders:


*

*It's a standard trick using elementary multivariable calculus and converting to polar coordinates (see, e.g., this answer) to show that the Gaussian integral has value
$$\require{cancel}\int_{-\infty}^{\infty} e^{-t^2} dt = \sqrt{\pi} .$$
Now, applying integration by parts with $u = e^{-t^2}, dv = dt$ to the above integral gives $du = -2 t e^{-t^2} dt, v = t$ and hence
$$\sqrt{\pi} = \cancelto{0}{\left.\left(e^{-t^2}\right)(t)\right\vert_{-\infty}^{\infty}} - \int_{-\infty}^{\infty} (t)(-2t e^{-t^2}) dt = 2 \int_{-\infty}^{\infty} t^2 e^{-t^2} dt,$$
and so
$$\int_{-\infty}^{\infty} t^2 e^{-t^2} dt = \frac{\sqrt{\pi}}{2} .$$
Now, substituting $t = \frac{x}{\sqrt{2}}, dt = \frac{dx}{\sqrt{2}}$ in the integral gives
$$\frac{1}{2 \sqrt{2}} \int_{-\infty}^{\infty} x^2 e^{-\frac{x^2}{2}} dx = \frac{\sqrt{\pi}}{2},$$
and multiplying by $\frac{2}{\sqrt{\pi}}$ gives the desired result:
$$\color{#bf0000}{\boxed{\int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx = 1}} .$$

*We can apply the "multivariable polar" trick directly to the integral at hand rather than to the Gaussian integral: If one denotes $$I := \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx ,$$ we have
$$
I^2
= \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx \cdot \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} y^2 e^{-\frac{y^2}{2}} dy
= \frac{1}{2 \pi} \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} x^2 y^2 e^{-\frac{x^2 + y^2}{2}} dx \, dy .
$$
Now, converting to polar coordinates gives
\begin{align}
I^2
&= \frac{1}{2 \pi} \int_0^{2 \pi} \int_0^{\infty} (r \cos \theta)^2 (r \sin \theta)^2 e^{-\frac{r^2}{2}} \cdot r \,dr \,d\theta \\
&= \frac{1}{2 \pi} \int_0^{2 \pi} \int_0^{\infty} r^5 e^{-\frac{r^2}{2}} \sin^2 \theta \cos^2 \theta \,dr \,d\theta \\
&= \frac{1}{2 \pi} \int_0^{2 \pi} \sin^2 \theta \cos^2 \theta \,d\theta \int_0^{\infty} r^5 e^{-\frac{r^2}{2}} dr .
\end{align}
Now, the first integral is a standard exercise that can be handled with double-angle identities and has value $\frac{\pi}{4}$. The second integral can be evaluated readily with the substitution $u = -\frac{r^2}{2}, du = -r \,dr$ and has value $8$, so
$$I^2 = \frac{1}{2 \pi} \left(\frac{\pi}{4}\right) (8) = 1 .$$
On the other hand, the integrand in $I$ is everywhere nonnegative, and so again $I$ has value
$$\color{#bf0000}{\boxed{\int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx = 1}}$$
as claimed.

</s>