Evaluating a trigonometric integral by means of contour $\int_0^{\pi} \frac{\cos(4\theta)}{1+\cos^2(\theta)} d\theta$ I am studying for a qualifying exam, and this contour integral is getting pretty messy:
$\displaystyle I = \int_0^{\pi} \dfrac{\cos(4\theta)}{1+\cos^2(\theta)} d\theta $
I first notice that the integrand is an even function hence 
$\displaystyle I = \dfrac{1}{2} \int_{-\pi}^{\pi} \dfrac{\cos(4\theta)}{1+\cos^2(\theta)} d\theta $
Then make the substitutions $\cos(n\theta) = \dfrac{e^{in\theta}+e^{-in\theta}}{2}$, and $z=e^{i\theta}$ to obtain:
$\displaystyle I = \dfrac{1}{2} \int_{||z||=1} \dfrac{\dfrac{z^4+z^{-4}}{2}}{1+\left(\dfrac{z+z^{-1}}{2}\right)^2} \dfrac{-i}{z}dz =  -i \int_{||z||=1} \dfrac{z^8+1}{z^3(z^4+6z^2+1)} dz$
Now, assuming this is right so far, this seems "straight-forward" in the sense that I know what to do. However, it gets ugly. I was hoping there might be a better method to evaluate this integral.
Thanks

Note that $\sin 4\theta$ is an odd function, so we can simplify to
$$\begin{align}
I &= \frac12 \int_{-\pi}^\pi \frac{e^{4i\theta}}{1 + \cos^2\theta}\,d\theta\\
&= \frac{1}{2i}\int_{\lvert z\rvert = 1} \frac{z^4}{1 + \left(\frac{z+z^{-1}}{2}\right)^2}\,\frac{dz}{z}\\
&= \frac{1}{2i}\int_{\lvert z\rvert = 1} \frac{z^5}{z^2+\frac14(z^2+1)^2}\,dz\\
&= -2i \int_{\lvert z\rvert = 1} \frac{z^5}{z^4 + 6z^2+1}\,dz.
\end{align}$$
That looks a little simpler to my untrained eye.
Then we need the zeros of the denominator, which are $\pm \sqrt{-3\pm\sqrt{8}}$, where the inner square root shall be the positive, and the outer can be either square root. The zeros inside the unit disk are $\zeta_\pm = \pm i\sqrt{3-\sqrt{8}}$, both are simple, so
$$\operatorname{Res}\left(\frac{z^5}{z^4+6z^2+1};\,\zeta_\pm\right) = \frac{\zeta_\pm^5}{4\zeta_\pm^3 + 12\zeta_\pm} = \frac{\zeta_\pm^4}{4\zeta_\pm^2 + 12} = \frac{(\sqrt{8}-3)^2}{4(\sqrt{8}-3)+12}=\frac{17-12\sqrt{2}}{8\sqrt{2}},$$
both residues are the same, and
$$I = 8\pi \frac{17-12\sqrt{2}}{8\sqrt{2}} = \pi\frac{17-12\sqrt{2}}{\sqrt{2}} = \frac{\pi}{24+17\sqrt{2}},$$
if I haven't miscalculated.
