Solving integral related to second moment of normal distribution restricted to interval. CAS answer acceptable Let $f(x)=\frac{1}{\sigma\sqrt{2\pi}}\, e^{-\frac{(x - \mu)^2}{2 \sigma^2}}$, the pdf of the 1-dimensional normal distribution.
Is it possible to compute $\int_{-a}^a x^2 \frac{1}{\sigma\sqrt{2\pi}}\, e^{-\frac{(x - \mu)^2}{2 \sigma^2}}dx$?
In other words we're looking at the concentration of the second moment.
I'm happy to know the answer for the case where $\mu=0$.
 A: You can perform the change of variable: $x-\mu=y$, then
$$
\begin{eqnarray}
\frac{1}{\sigma\sqrt{2\pi}}\int_{-a}^a x^2 e^{-\frac{(x - \mu)^2}{2 \sigma^2}}dx&=&
\color{#00f}{
\frac{1}{\sigma\sqrt{2\pi}}\int_{-a- \mu}^{a- \mu} y^2 e^{-\frac{y^2}{2 \sigma^2}}dy}+\\&+&
\color{#0a0}{
\frac{2\mu}{\sigma\sqrt{2\pi}}\int_{-a- \mu}^{a- \mu} ye^{-\frac{y^2}{2 \sigma^2}}dy}+\\&+&
\color{#f00}{\frac{\mu^2}{\sigma\sqrt{2\pi}}\int_{-a- \mu}^{a- \mu}e^{-\frac{y^2}{2 \sigma^2}}dy}.
\end{eqnarray}
$$
The last one is  computed (directly by the definition) in terms of error functions:
$$\color{#f00}{\frac{\mu^2}{\sigma\sqrt{2\pi}}\int_{-a- \mu}^{a- \mu}e^{-\frac{y^2}{2 \sigma^2}}dy}=\frac{\mu ^2 }{2}\left(\text{erf} \left(\frac{a-\mu}{\sqrt{2} \sigma} \right)+\text{erf}\left(\frac{a+\mu}{\sqrt{2} \sigma} \right)\right)$$
The second integral can be computed via change of variable: $y^2=t$ (and get a simple exponential integral):
$$
\color{#0a0}{\frac{2\mu^2}{\sigma\sqrt{2\pi}}\int_{-a- \mu}^{a- \mu}y e^{-\frac{y^2}{2 \sigma^2}}dy}=
\frac{\mu^2}{\sigma\sqrt{2\pi}}\int_{(a+ \mu)^2}^{(a- \mu)^2}e^{-\frac{t}{2 \sigma^2}}dt=\sqrt{\frac{2}{\pi }} \mu  \sigma  \left(e^{-\frac{(a+\mu )^2}{2 \sigma ^2}}-e^{-\frac{(a-\mu )^2}{2 \sigma ^2}}\right)
$$
The first one can be handled in different ways. For example, one can assume a differentiation under the integral. One can set $\frac{1}{\sigma^2}=\alpha$:
$$
\int_{-a- \mu}^{a- \mu} y^2 e^{-\frac{\alpha \ y^2}{2}}dy=-2\int_{-a- \mu}^{a- \mu}\frac{\partial e^{-\frac{\alpha \ y^2}{2}}}{\partial \alpha}dy=-2\frac{\partial}{\partial \alpha} \int_{-a- \mu}^{a- \mu} e^{-\frac{\alpha \ y^2}{2}}dy.$$
And the last-mentioned integral was already computed in terms of error functions. Then just use the differentiation of error functions, perform the back substitution ($\alpha=\frac{1}{\sigma^2}$) and do not forget about the multiplier $\frac{1}{\sigma\sqrt{2\pi}}$ and you are ready to collect the result:
$$\!\color{#00f}{\frac{1}{\!\sigma\sqrt{2\pi}}\!\int_{-a- \mu}^{a- \mu}\! y^2 e^{-\frac{y^2}{2 \sigma^2}}dy\!}=\!
\frac{\sigma ^2}{2}\!\!\left(\!
\text{erf}\!\left(\!\frac{\!a+\mu \!}{\sqrt{2} \sigma }\!\right)\!+\!\text{erf}\!\left(\!\frac{a-\mu }{\sqrt{2} \sigma }\!\!\right)
\!\!\right)\!-
\!\frac{\!\sigma\! }{\sqrt{2 \pi }}\!\left(\!\!
(a-\mu ) e^{-\frac{(a-\mu )^2}{2 \sigma ^2}}\!+\!(\!a\!+\!\mu\!) e^{-\frac{(a+\mu )^2}{2 \sigma ^2}}\!
\right).\!
$$
