Complex analysis - Finding $\int_0^\infty \frac{x\cos\left(\frac{1}{x^2}\right)}{x^4 + 4}\,dx$

I want to use complex analysis to solve this integral:

$$ I = \int_0^\infty \frac{x\cos\left(\frac{1}{x^2}\right)}{x^4 + 4}\,dx$$

I'm having trouble because I get two different results with two different methods.

First of all, I applied the change of variables $\displaystyle y = \frac{2}{x^2}$ to get that
$\displaystyle I = \frac{1}{4} \int_0^\infty \frac{\cos(\frac{y}{2})}{y^2 + 1}dy = \frac{1}{4} \Re \left( \int_0^\infty \frac{e^{iy/2}}{y^2 + 1}dy \right)$ which is a well known integral and is equal to $\displaystyle \frac{\pi}{2\sqrt e}$.

So $\displaystyle I = \frac{\pi}{8\sqrt e}$. This is the correct result.

(For those who don't know, the integral can be solved using Feynman's Trick or Jordan's Lemma with a suitable contour in the upper half plane.)

Now, let's say that I want to solve the integral without making any change of variables or others tricks. I just want to solve it using the residue theorem with a quarter of circle of radius $\displaystyle R \to \infty$ and center $\displaystyle C = (0,0)$ in the first quadrant as a contour. Let's call this contour $\displaystyle \gamma$.

So I want to find $\displaystyle J =\oint_\gamma \frac{z\cos(\frac{1}{z^2})}{z^4 + 4}dz$ and how it is related to I. Let's find J.

$\displaystyle J =\int_0^\infty \frac{z\cos(\frac{1}{z^2})}{z^4 + 4}dz + \int_{Arc} \frac{z\cos(\frac{1}{z^2})}{z^4 + 4}dz + \int_{i\infty}^{i0} \frac{z\cos(\frac{1}{z^2})}{z^4 + 4}dz$,

where Arc is parametrized by $\displaystyle z(\theta) = R e^{i\theta}$ for $\displaystyle \theta \in \left [0,\frac{\pi}{2} \right]$.

The first integral is obviously I and so is the third ( use the change of variables $\displaystyle z \to -iz$ ). Let's call the second integral $\displaystyle I_{Arc}$.

EDIT: After a comment I decided to work on the third piece:

$\displaystyle Q = \int_{i\infty}^{i0} \frac{z\cos(\frac{1}{z^2})}{z^4 + 4}dz$

using the change of variables $\displaystyle q = -iz$

$\displaystyle Q = \int_{\infty}^{0} \frac{iq\cos\left(\frac{1}{(iq)^2}\right)}{(iq)^4 + 4}idq = -\int_{\infty}^{0} \frac{q\cos\left(\frac{-1}{q^2}\right)}{(iq)^4 + 4}dq = \int_{0}^{\infty} \frac{q\cos\left(\frac{1}{q^2}\right)}{q^4 + 4}dq = I $

Now, I would like to show that $\displaystyle I_{Arc} \to 0$ as $\displaystyle R \to \infty$.

$\displaystyle \mid I_{Arc} \mid = \left| \int_0^{\frac{\pi}{2}} \frac{R e^{i\theta} \cos\left(\frac{1}{R^2 e^{i2\theta}}\right)}{R^4 e^{i4\theta} + 4} R e^{i\theta} i d\theta \right| \leq \int_0^{\frac{\pi}{2}} \left| \frac{R^2 \cos\left(\frac{1}{R^2 e^{i2\theta}}\right)}{R^4 e^{i4\theta} + 4} \right| d\theta \leq \frac{3R^2}{R^3} \int_0^{\frac{\pi}{2}} d\theta = \frac{1}{R}\frac{3\pi}{2} \to 0$

as $\displaystyle R \to \infty$

Where I used: $\displaystyle \left| \cos\left(\frac{1}{R^2 e^{i2\theta}}\right) \right| = \left| \frac{ \exp\left(\frac{i\cos(2\theta) + \sin(2\theta)}{R^2} \right) + \exp\left(\frac{-i\cos(2\theta) - \sin(2\theta)}{R^2} \right) }{2} \right| \leq \left| \frac{ \exp\left(\frac{\sin(2\theta)}{R^2} \right)}{2} \right| + \left| \frac{ \exp\left(\frac{-\sin(2\theta)}{R^2} \right)}{2} \right| \leq \exp\left(\frac{\sin(2\theta)}{R^2} \right) \leq 3$
since R is definitely bigger than 1.

Also

$\displaystyle \mid R^4e^{i4\theta} + 4 \mid = \sqrt{(R^4\cos4\theta + 4)^2 + R^4\sin^24\theta} = \sqrt{R^8 + 16 + 8R^4\cos4\theta} \ge \sqrt{R^8 + 8R^4\cos4\theta} \ge \sqrt{R^8 - 8R^4} \ge R^3$.

Which is true if $\displaystyle R >> 1$.

So I changed how I bounded $\displaystyle \mid I_{Arc} \mid$

So, it stands to reason that $\displaystyle J = 2I$ and $\displaystyle J = 2\pi i \sum\operatorname{Res}(f,z_k)$, but the only pole inside the region bounded by $\displaystyle \gamma $ is $\displaystyle z = 1 + i$, so after some algebra, one can find that $\displaystyle J = \frac{\pi}{8}\cosh\frac{1}{2}$.

So, $\displaystyle I = \frac{\pi}{8} \left(\sqrt{e} + \frac{1}{\sqrt{e}} \right)$.

What I am doing wrong?

(I know that I may have written too much, but please don't bash me too harshly, this is my first time writing a question).

EDIT:

For those who don't want to read the comments.

$\displaystyle z = 0$ is an essential singularity, so as the singularity is on the contour I need to make a detour around $\displaystyle z = 0$. For example I can use the path parametrized by $\displaystyle \sigma(\theta) = \epsilon e^{i\theta}$ for $\displaystyle \theta \in \left[0,\frac{\pi}{2}\right]$ and $\displaystyle \epsilon << 1$ which contributes to the value of $\displaystyle J$. But, now the curve is oriented clock-wise. So, $\displaystyle J = 2I + $ this contribution. After I calculate this contribution I will edit the question again.

Final EDIT ( I suppose ):

Calculating this contribution is just like calculating manually $I_{Arc}$ but in the limit of $R \to 0$, I also suppose that this is not analytically doable, I guess that a simple Monte Carlo Integration technique could help. Anyway, this contribution should make things work. The moral of the story is to trust in the power of analysis and to do things in a smart way just like I did in the first part of this question.


To evaluate this integral using a quarter-circle contour in the first quadrant of the complex plane (that is indented at the origin), we can exploit the fact that if we're in the first quadrant , then $$ \left|\exp \left(\color{red}{-}\frac{i}{z^{2}} \right) \right| = \left|\exp \left(\color{red}{-}\frac{i}{(x+iy)^2} \right) \right| = \exp \left(\color{red}{-}\frac{2xy}{(x^{2}+y^{2})^{2}} \right) \le 1.$$

In other words, the magnitude of $\exp \left(\color{red}{-}\frac{i}{z^{2}} \right)$ remains bounded in we stay in the first quadrant, which includes near the essential singularity at the origin.

Notice the negative sign. This is not true for $\exp \left(\frac{i}{z^{2}} \right)$, and thus not true for $\cos \left(\frac{1}{z^{2}} \right).$

Therefore, if we integrate $$f(z) = \frac{z\exp\left(\color{red}{-}\frac{i}{z^{2}}\right)}{z^{4}+4} $$ around the contour, the integral along the big arc and the integral along the small arc will both vanish in the limit.

We're then left with $$\begin{align} \int_{0}^{\infty} \frac{x \exp \left(\color{red}{-}\frac{i}{x^{2}}\right)}{x^{4}+4} \, \mathrm dx + \int_{\infty}^{0} \frac{(it) \exp \left( \frac{i}{t^{2}}\right)}{t^{4}+4} \, (i \, \mathrm dt) &= 2 \int_{0}^{\infty} \frac{x \cos \left(\frac{1}{x^{2}} \right)}{x^{4}+4} \, \ \mathrm dx \\ &= 2 \pi i \operatorname{Res}[f(z),1+i] \\ & = \frac{\pi}{4 \sqrt{e}}. \end{align} $$

I asked a question about how to deal with essential singularities that are on the contour several years ago.

See here.


EDIT:

If we wanted to integrate the function $\frac{z\exp\left(\frac{i}{z^{2}}\right)}{z^{4}+4}$ instead, we could use a quarter-circle in the fourth quadrant.