A complex function integral I got a problem in an exam.
We need to caculate following limit:

$$\lim_{x\rightarrow \infty} \int_{L_x}\frac{\cos z}{z^2+1} dz,$$
  where $L_x$ is the line from $-x+2i$ to $x+2i$.

Of course we can calculate the integrals of the real part and imaginary part seperately. But it would be extremely complicated. I think it can't be the right solution since it wouldn't be done in the time of an exam.
 A: First note that we can write
$$\lim_{x\to \infty}\int_{L_x}\frac{\cos(z)}{z^2+1}\,dz=\lim_{x\to \infty}\left(\frac12\int_{L_x}\frac{e^{iz}}{z^2+1}\,dz+\frac12\int_{L_x}\frac{e^{-iz}}{z^2+1}\,dz\right)\tag 1$$
We can evaluate the limit of each of the integrals on the right-hand side of $(1)$ using the residue theorem. 

For the first integral, we close the contour in the upper-half plane.  Inasmuch as neither pole is enclosed by the contour, we see that 
$$\lim_{x\to \infty}\left(\frac12\int_{L_x}\frac{e^{iz}}{z^2+1}\,dz\right)=0$$

For the second integral, we close the contour in the lower-half plane.  Inasmuch as both poles are enclosed, we have
$$\begin{align}
\lim_{x\to \infty}\left(\frac12\int_{L_x}\frac{e^{-iz}}{z^2+1}\,dz\right)&=-\pi i \text{Res}\left(\frac{e^{-iz}}{z^2+1}, z=\pm i\right)\\\\
& =-\pi i \left(\frac{e}{2i}+\frac{e^{-1}}{-2i}\right)\\\\
&=-\pi \sinh(1)
\end{align}$$

Putting it all together, we see that 
$$\bbox[5px,border:2px solid #C0A000]{\lim_{x\to \infty}\int_{L_x}\frac{\cos(z)}{z^2+1}\,dz=-\pi \sinh(1)}$$
A: Let us consider a rectangle $\gamma_R$ with vertices at $-R,R,R+2i,-R+2i$, counter-clockwise oriented, with $R\gg 1$and $f(z)=\frac{\cos(z)}{z^2+1}$. By the ML lemma and the residue theorem
$$\begin{eqnarray*}\oint_{\gamma_R}f(z)\,dz &=& \int_{-R}^{+R}\frac{\cos(z)}{z^2+1}\,dz - \int_{-R+2i}^{R+2i}f(z)\,dz + o(1)\\&=& 2\pi i\cdot\text{Res}\left(f(z),z=i\right)=\pi\cosh(1) \end{eqnarray*}$$
as $R\to +\infty$, hence in order to compute the given limit it is enough to compute
$$ \lim_{R\to +\infty}\int_{-R}^{+R}\frac{\cos(z)}{z^2+1}\,dz =\text{Re}\left[2\pi i\cdot \text{Res}\left(\frac{e^{iz}}{z^2+1},z=i\right)\right]=\frac{\pi}{e}$$
to get:
$$ \lim_{R\to +\infty}\int_{-R+2i}^{R+2i}f(z)\,dz = \color{red}{-\pi\sinh(1)}.$$
