How do I use residues to evaluate this integral I had a question in the exercises of a complex analysis course I couldn't solve, It asked me to evaluate this integral $$\int_{-\pi}^{\pi}\frac{dx}{\cos^2(x) + 1}$$
I tried to evaluate it without using residues, the antiderivative of this function contains tan, which is not defined at $\pm \pi/2 $
all the examples I have worked with so far are rational functions (a polynomial over another) 
 A: First start by:
$$\int _{-\pi }^{\pi }\:\frac{dx}{\left(\cos \left(x\right)\right)^2+1}=4\int _{0\:}^{\frac{\pi }{2}\:}\:\frac{dx}{\left(\cos \:\left(x\right)\right)^2+1}=4\int _{0\:}^{\frac{\pi \:}{2}\:}\:\frac{\left(\sec \left(x\right)\right)^2dx}{2+\left(\tan \left(x\right)\right)^2}$$ 
Then using substitution $$u\sqrt{2}=\tan \left(x\right)$$
We get:
$$2\sqrt{2}\int _0^{\infty }\:\frac{du}{1+u^2}=\pi \sqrt{2}$$
A: Here's an answer using residues.
Using
$cos(x) = \frac{1}{2}\left(e^{ix} + e^{-ix}\right)$
and defining $z=e^{ix}$ which implies $dz = izdx$, we write the integral as
$$\int_{-\pi}^{\pi} \frac{1}{(cos(x))^2+1} dx$$
$$=\int_{-\pi}^{\pi} \frac{4}{z^2+6+1/z^2} \frac{dz}{iz}$$
$$=\frac{1}{i}\int_{-\pi}^{\pi} \frac{4z}{z^4+6z^2+1} dz. $$
We so obtained an integral along the unit circle!
Next, we need to find all residues inside the unit circle.
For this, we define $g(z) = z^4+6z^2+1$. Factoring out gives
$$g(z) = (z^2 + 3 + 2\sqrt{2})\ldots\\ \ldots\left(z-i\sqrt{3-2\sqrt{2}}\right) \left(z+i\sqrt{3-2\sqrt{2}}\right). $$
Hence, there are two roots inside the unit circle, 
$$z_1 =i\sqrt{3-2\sqrt{2}}$$
and 
$$z_2=-i\sqrt{3-2\sqrt{2}}$$
(and two outside coming from the first bracket but we don't have to care about those). And it is for these two roots that we have to find the residues.
Defining $f(z) = 4z$, we see that our integrand is a rational function, $f/g$. Moreover, $z_1$ and $z_2$ are poles of order 1 of $f/g$. For this case, there is a well known result giving us the residues for $f/g$ at these poles, namely
$$Res\left(\frac{f}{g}, z_k\right)=\frac{f(z_k)}{g'(z_k)}$$
where $k\in\{1, 2\}$. 
Since $g'(z) = 4z^3 + 12z$, we obtain:
$$Res\left(\frac{f}{g}, z_k\right)=\ldots=\frac{1}{z_k^2 + 3}=$$
$$\ldots=\frac{1}{2\sqrt{2}}$$
(yep, the same for both $k$). 
We conclude by applying the residue theorem:
$$\frac{1}{i}\int_{-\pi}^{\pi} \frac{4z}{z^4+6z^2+1} dz$$
$$=\frac{1}{i}\left(2\pi i \sum_{k=1}^{2}Res\left(\frac{f}{g}, z_k\right)\right) $$
$$=\frac{1}{i}\left(2\pi i \frac{2}{2\sqrt{2}}\right)$$
$$=\pi \sqrt{2}. $$
