How to evaluate this improper integral $\int_{-\infty}^\infty x^2 e^{-x^2}\cos x \, dx$? How to evaluate
$$
\int_{-\infty}^{\infty} x^2 e^{-x^2} \cos x \, dx?
$$
My Attempt:

Since the integrand is an even function, we have
$$
\int_{-\infty}^{\infty} x^2 e^{-x^2} \cos x \, dx = 2 \int_0^{\infty} x^2 e^{-x^2} \cos x \, dx. \tag{1} 
$$


Since $-1 \leq \cos x \leq 1$ and $x^2 e^{-x^2} > 0$ for all $x$ such that $0 \leq  x < \infty$, we have
$$
-x^2 e^{-x^2} \leq x^2 e^{-x^2} \cos x \leq x^2 e^{-x^2},
$$
which implies
$$
-\int_0^{\infty} x^2 e^{-x^2} \, dx \leq \int_0^{\infty} x^2 e^{-x^2} \cos x \, dx \leq \int_0^{\infty} x^2 e^{-x^2} \, dx, 
$$
and hence
$$
-2 \int_0^{\infty} x^2 e^{-x^2} \, dx \leq 2 \int_0^{\infty} x^2 e^{-x^2} \cos x \, dx \leq 2 \int_0^{\infty} x^2 e^{-x^2} \, dx.
$$

Now if we can show that
$$
\int_0^{\infty} x^2 e^{-x^2} \, dx = 0, 
$$
then would obtain
$$
\int_{-\infty}^{\infty} x^2 e^{-x^2} \cos x \, dx = 0.
$$
[ Refer to (1) above.]
Am I right? If so, then what next? How to proceed?
Or, is there another better way around this problem?
 A: You could approach this with a recursive diffeq method. This is a bit inefficient but with enough effort this works.
Let $$I(a)=\int^{\infty}_{-\infty}x^2 e^{-x^2}\cos(ax)\text{ d}x$$
Then
\begin{align}
I'(a)&=-\int^{\infty}_{-\infty}x^3e^{-x^2}\sin(ax)=-\int^{\infty}_{-\infty}\underbrace{x^2\sin(ax)}_u\cdot \underbrace{xe^{-x^2}}_v\text{ d}x\\
&=\left.x^2\sin(ax)\cdot\frac{e^{-x^2}}{2}\right|_{-\infty}^{\infty}-\int^{\infty}_{-\infty}xe^{-x^2}\sin(ax)\text{ d}x-\frac{a}{2}I(a)\\
&=-\color{red}{\int^{\infty}_{-\infty}xe^{-x^2}\sin(ax)\text{ d}x}-\frac{a}{2}I(a)\\
&=-\color{red}{\frac12 a\sqrt\pi e^{\frac{-a^2}4}}-\frac{a}{2}I(a)
\end{align}
To find the red integral, recursively apply the same method we did to $I(a)$ another time.
Now, we have a diffeq, so we will find the initial condition $$I(0)=\color{blue}{\int^{\infty}_{-\infty} x^2e^{-x^2}\text{ d}x}=\color{blue}{\frac{\sqrt\pi}2}$$
to find this, solving it using an antiderivative and FTC honestly isn't that hard.
Solving the diffeq will yield $$I(a)=-\frac14 \sqrt\pi(-2 + a^2) e^{\frac{-a^2}4}$$
Hence $$I(1)=\frac{\sqrt{π}}{4\sqrt[4]{e}}$$
This was the initial method I used and honestly it is rather tedious. I would definitely recommend another approach probably using complex analysis with a rectangular contour or of the like.
A: Consider the problem of the antiderivative
$$I=\int x^2 e^{-x^2} \cos (x) \, dx=\Re \int x^2 e^{-x^2} e^{i x} \, dx=\Re \int x^2 e^{-x^2+ix}  \, dx$$ Complete the square
$$J=\int x^2 e^{-x^2+ix}  \, dx=e^{-\frac 14} \int x^2 e^{-\left(x-\frac{i}{2}\right)^2}\,dx$$ Now
$$x=t+\frac i 2 \implies K=\int x^2 e^{-\left(x-\frac{i}{2}\right)^2}\,dx=\int \Big[ e^{-t^2} t^2+i e^{-t^2} t-\frac{1}{4}e^{-t^2}\Big]\,dt$$  One integration by parts for the first one leads to
$$K=\frac{1}{4} \left(\frac{1}{2} \sqrt{\pi }\, \text{erf}(t)-2
   e^{-t^2} (t+i)\right)$$ Back to $J$
$$J=-\frac{i \sqrt{\pi } \text{erfi}\left(\frac{1}{2}+i x\right)}{8
   \sqrt[4]{e}}-\frac{1}{4} e^{-x (x-i)} (2 x+i)$$
$$I=\frac{1}{16} \left(\frac{\sqrt{\pi } \text{erf}\left(\frac{1}{2}
   (2 x+i)\right)}{\sqrt[4]{e}}-\frac{i \sqrt{\pi }
   \text{erfi}\left(\frac{1}{2}+i x\right)}{\sqrt[4]{e}}-2 e^{-x
   (x+i)} \left(2 x+e^{2 i x} (2 x+i)-i\right)\right)$$ Using the bounds, the result already given by @Max0815 using a much more elegant approach.
A: How to solve!
The actual result is different:
$$ \int_{-\infty}^{\infty} x^{2} \cdot e^{-x^{2}} \cdot \cos(x) ~ \mathrm{d}x = \frac{\sqrt{\pi}}{4 \cdot \sqrt[4]{e}} \approx 0.345097 $$
The easiest way to solve the problem would be to form one od the antiderivatives:
(You can easily do this by relating the trigonometric functions to the exponential functions:
$$ \cos(x) = \frac{e^{x \cdot \mathrm{i}} + e^{-x \cdot \mathrm{i}}}{2} $$
$$ \int x^{2} \cdot e^{-x^{2}} \cdot \cos(x) ~ \mathrm{d}x = \dfrac{\mathrm{e}^{-\frac{1}{4}}\cdot\left(\sqrt{{\pi}} \cdot \operatorname{erf}\left(x+\frac{\mathrm{i}}{2}\right)+\sqrt{{\pi}} \cdot \operatorname{erf}\left(x-\frac{\mathrm{i}}{2}\right)\right)}{16}+\dfrac{\mathrm{e}^{-x^2}\cdot\left(\sin\left(x\right)-2 \cdot x \cdot \cos\left(x\right)\right)}{4} $$
or
$$ \int x^{2} \cdot e^{-x^{2}} \cdot \cos(x) ~ \mathrm{d}x = 
\dfrac{\frac{\mathrm{e}^{-\frac{1}{4}}\mathrm{i}\cdot\left(\frac{\mathrm{i}\operatorname{\Gamma}\left(\frac{1}{2},\frac{\left(\mathrm{i}-2x\right)^2}{4}\right)\left(\mathrm{i}-2x\right)}{4\sqrt{\left(\mathrm{i}-2x\right)^2}}-\frac{\mathrm{i}\operatorname{\Gamma}\left(\frac{3}{2},\frac{\left(\mathrm{i}-2x\right)^2}{4}\right)\left(\mathrm{i}-2x\right)^3}{\left(\left(\mathrm{i}-2x\right)^2\right)^\frac{3}{2}}-\operatorname{\Gamma}\left(1,\frac{\left(\mathrm{i}-2x\right)^2}{4}\right)\right)}{2}+\frac{\mathrm{e}^{-\frac{1}{4}}\mathrm{i}\cdot\left(\frac{\mathrm{i}\operatorname{\Gamma}\left(\frac{1}{2},\frac{\left(-2x-\mathrm{i}\right)^2}{4}\right)\left(-2x-\mathrm{i}\right)}{4\sqrt{\left(-2x-\mathrm{i}\right)^2}}-\frac{\mathrm{i}\operatorname{\Gamma}\left(\frac{3}{2},\frac{\left(-2x-\mathrm{i}\right)^2}{4}\right)\left(-2x-\mathrm{i}\right)^3}{\left(\left(-2x-\mathrm{i}\right)^2\right)^\frac{3}{2}}+\operatorname{\Gamma}\left(1,\frac{\left(-2x-\mathrm{i}\right)^2}{4}\right)\right)}{2}}{2} $$
(calculated by Integralrechner, wich shows a step by step solution too)
or
$$ \int x^{2} \cdot e^{-x^{2}} \cdot \cos(x) ~ \mathrm{d}x = \frac{(\frac{\sqrt{\pi} \cdot \operatorname{erf}(\frac{2 \cdot x + \mathrm{i}}{2})}{\sqrt[4]{e}} - \frac{\mathrm{i} \sqrt(\pi) \operatorname{erfi}\frac{1}{2} + \mathrm{i} \cdot x)}{\sqrt[4]{e}} - 2 e^{-x \cdot (x + \mathrm{i})} \cdot (2 x + e^{2 \cdot \mathrm{i} \cdot x} (2 \cdot x + \mathrm{i}) - \mathrm{i}))}{16} + constant $$
(calculated by Wolfram|Alpha)
or
...
Now you have to form limes with from x to infinity and x to -infinity of you're formula and you will get you're sulution! (as shown above)
$$ \int_{-\infty}^{\infty} x^{2} \cdot e^{-x^{2}} \cdot \cos(x) ~ \mathrm{d}x = \lim_{{x} \to {\infty}} (\dfrac{\mathrm{e}^{-\frac{1}{4}}\cdot\left(\sqrt{{\pi}} \cdot \operatorname{erf}\left(x+\frac{\mathrm{i}}{2}\right)+\sqrt{{\pi}} \cdot \operatorname{erf}\left(x-\frac{\mathrm{i}}{2}\right)\right)}{16}+\dfrac{\mathrm{e}^{-x^2}\cdot\left(\sin\left(x\right)-2 \cdot x \cdot \cos\left(x\right)\right)}{4}) - \lim_{{x} \to {-\infty}} (\dfrac{\mathrm{e}^{-\frac{1}{4}}\cdot\left(\sqrt{{\pi}} \cdot \operatorname{erf}\left(x+\frac{\mathrm{i}}{2}\right)+\sqrt{{\pi}} \cdot \operatorname{erf}\left(x-\frac{\mathrm{i}}{2}\right)\right)}{16}+\dfrac{\mathrm{e}^{-x^2}\cdot\left(\sin\left(x\right)-2 \cdot x \cdot \cos\left(x\right)\right)}{4})$$
A: By successive integration by parts, we will show that $\int_{-\infty}^\infty x^2 e^{-x^2} \cos(x)\,dx=1/4\int_{-\infty}^\infty e^{-x^2} \cos(x)\,dx$.  The latter integral can be evaluated a number of ways as shown Here.  We now proceed.

Let $I$ be the integral of interest given by
$$I=\int_{-\infty}^\infty x^2 e^{-x^2} \cos(x)\,dx\tag 1$$
Integrating by part the integral in $(1)$ with $u=x\cos(x)$ and $v=-\frac12e^{-x^2}$ reveals
$$\begin{align}
I&=\frac12 \int_{-\infty}^\infty e^{-x^2} (\cos(x)-x\sin(x))\,dx\\\\
&=\frac12 \int_{-\infty}^\infty e^{-x^2} \cos(x)\,dx-\frac12 \int_{-\infty}^\infty xe^{-x^2} \sin(x)\,dx\tag2
\end{align}$$
And integrating by parts the second integral in $(2)$ with $u=\sin(x)$ and $v=-\frac12e^{-x^2}$, we obtain
$$I=\frac14 \int_{-\infty}^\infty e^{-x^2} \cos(x)\,dx\tag 3$$
Using any of the methodologies Here to evaluate the integral in $(3)$, we arrive at the coveted solution
$$\int_{-\infty}^\infty x^2 e^{-x^2} \cos(x)\,dx=\frac14 \sqrt\pi e^{-1/4}$$
A: Let
$$ I(a)=\int_0^\infty e^{-x^2}\cos(ax)\,\mathrm{d}x. $$
Then
$$ I(a)=\frac{\sqrt\pi}{2}\,e^{-a^2/4} $$
from here
So
$$I''(a)=-\int_0^\infty x^2e^{-x^2}\cos(ax)\,\mathrm{d}x=\frac{d^2}{da^2}\frac{\sqrt\pi}{2}\,e^{-a^2/4}=\frac18(a^2-2)\sqrt\pi e^{-a^2/4}
$$
and hence
$$\int_0^\infty x^2e^{-x^2}\cos(x)\,\mathrm{d}x=-\frac18(a^2-2)\sqrt\pi e^{-a^2/4}\bigg|_{a=1}=\frac{\sqrt\pi}{8e^{1/4}}.
$$
A: First of all, let's consider the integral
$$
I_n=\int_0^{\infty}x^{2n}e^{-x^2}dx \quad \forall n \in \mathbb{N}.
$$
Notice that
$$
I_0=\int_0^{\infty}e^{-x^2}dx=\frac{\sqrt{\pi}}{2}.
$$
Using integration by parts we have
\begin{eqnarray}
I_n
&=&-\frac{1}{2}\int_0^{\infty}x^{2n-1}d\left(e^{-x^2}\right)\cr
&=&-\frac12 x^{2n-1}e^{-x^2}\Big|_0^{\infty}+\frac12\int e^{-x^2}d(x^{2n-1})\cr
&=&\frac{2n-1}{2}\int_0^{\infty}x^{2n-2}e^{-x^2}dx
\end{eqnarray}
i.e.
$$
I_n=\frac{2n-1}{2}I_{n-1}
$$
It follows that
\begin{eqnarray}
I_n&=&\left(\frac{2n-1}{2}\right)\left(\frac{2n-3}{2}\right)\cdots\left(\frac{3}{2}\right)\left(\frac{1}{2}\right)I_0\cr
&=&\frac{1\cdot3\cdots(2n-1)}{2^n}I_0\cr
&=&\frac{1\cdot2\cdot3\cdot4\cdot5\cdots(2n-1)\cdot(2n)}{2^n[2\cdot4\cdot6\cdots(2n)}I_0\cr
&=&\frac{(2n)!}{2^{2n}n!}I_0\cr
&=&\frac{(2n)!}{4^n\cdot n!}\cdot\frac{\sqrt{\pi}}{2}.
\end{eqnarray}
Now, we have
\begin{eqnarray}
I
&:=&\int_{-\infty}^{\infty}x^2e^{-x^2}\cos(x)dx\cr
&=&2\int_0^{\infty}x^2e^{-x^2}\cos(x)dx\cr
&=&2\int_0^{\infty}\sum_{n=0}^{\infty}\frac{(-1)^n}{(2n)!}x^{2n+2}e^{-x^2}dx\cr
&=&2\sum_{n=0}^{\infty}\frac{(-1)^n}{(2n)!}\int_0^{\infty}x^{2n+2}e^{-x^2}dx\cr
&=&2\sum_{0}^{\infty}\frac{(-1)^n}{(2n)!}I_{n+1}\cr
&=&2\sum_{n=0}^{\infty}\frac{(-1)^n}{(2n)!}\cdot\frac{(2n+2)!}{4^{n+1}\cdot(n+1)!}\cdot\frac{\sqrt{\pi}}{2}\cr
&=&\frac{\sqrt{\pi}}{2}\sum_{n=0}^{\infty}\frac{\left(-\frac14\right)^n(2n+1)}{ n!}\cr
&=&\frac{\sqrt{\pi}}{2}u\left(-\frac{1}{4}\right)
\end{eqnarray}
with
\begin{eqnarray}
u(x)&=&\sum_{n=0}^{\infty}\frac{2n+1}{n!}x^n\cr
&=&2\sum_{n=0}^{\infty}\frac{nx^n}{n!}+\sum_{n=0}^{\infty}\frac{x^n}{n!}\cr
&=& e^x+2\sum_{n=1}^{\infty}\frac{x^n}{(n-1)!}\cr
&=& e^x+2\sum_{k=0}^{\infty}\frac{x^{k+1}}{k!}\cr
&=& e^x+2x\sum_{k=0}^{\infty}\frac{x^k}{k!}\cr
&=&(1+2x)e^x
\end{eqnarray}
So
$$
\int_{-\infty}^{\infty}x^2e^{-x^2}\cos(x)dx=\frac{\sqrt{\pi}}{2}\left(1-\frac{2}{4}\right)e^{-1/4}=\frac{1}{4}\sqrt{\pi}e^{-\frac{1}{4}}.
$$
A: First notice that $\int_{-\infty}^{+\infty}x^2e^{-x^2}\sin x\,\mathrm dx=0$ (by imparity) hence
$$I:=\int_{-\infty}^{+\infty}x^2e^{-x^2}\cos x\,\mathrm dx=\int_{-\infty}^{+\infty}x^2e^{-x^2+ix}\,\mathrm dx.$$
An integration by part gives
$$\int_{-\infty}^{+\infty}x\left(-2x+i\right)e^{-x^2+ix}\,\mathrm dx=-\int_{-\infty}^{+\infty}e^{-x^2+ix}\,\mathrm dx,$$
from which we deduce (using $\int_{-\infty}^{+\infty}\left(-2x+i\right)e^{-x^2+ix}\,\mathrm dx=0$ next):
$$\begin{align}I&=\int_{-\infty}^{+\infty}\frac{1+ix}2e^{-x^2+ix}\,\mathrm dx\\&=\int_{-\infty}^{+\infty}\frac{1+i\frac i2}2e^{-x^2+ix}\,\mathrm dx\\&=\frac14\int_{-\infty}^{+\infty}e^{-x^2+ix}\,\mathrm dx\\
&=\frac14\sqrt\pi\;e^{-1/4}.\end{align}$$
