Trigonometric Integral - Complex Integration Exercise :
If $a \in \mathbb R-\{0\}, |a|<1$, calculate the integral and show that :
$$\int_0^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta = \frac{\pi}{\sqrt{1-a^2}}\Bigg(\frac{\sqrt{1-a^2} -1}{a} \Bigg)^n$$
Attempt :
Since $\cos \theta$ is even, we have :
$$\int_0^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta = \frac{1}{2}\int_{-\pi}^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta$$ 
We substitute $\cos n\theta = \Re(e^{in\theta})$ and we get :
$$\int_0^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta = \frac{1}{2}\int_{-\pi}^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta = \frac{1}{2}\Re\Bigg(\int_{-\pi}^\pi \frac{e^{in\theta}}{1 + a\cos \theta}d\theta \Bigg) $$ 
Now, I understand that a contour integral can be made around the unit circle $|z|=1$ and find the singularities to work with residues, but I cannot seem how to move one with this procedure.
I would really appreciate a thorough solution or some hints on how to get to the answer.
 A: Let $z=e^{i\theta}$ and then
\begin{eqnarray}
&&\int_0^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta\\
& =& \frac{1}{2}\int_{-\pi}^\pi \frac{\cos n\theta}{1 + a\cos \theta}d\theta\\
&=&\frac12\int_{|z|=1}\frac{\frac{z^n+\frac1{z^n}}{2}}{1+a\frac{z+\frac1z}{2}}\frac1{iz}dz\\
&=&\frac1{2i}\int_{|z|=1}\frac{z^{2n}+1}{z^n(2z+a(z^2+1))}dz\\
&=&\frac1{2ai}\int_{|z|=1}\frac{z^{2n}+1}{z^n(z-z_1)(z-z_2)}dz\\
&=&\frac1{2ai}2\pi i\bigg[\textbf{Res}\bigg(\frac{z^{2n}+1}{z^n(z-z_2)},z=z_1\bigg)+\textbf{Res}\bigg(\frac{z^{2n}+1}{z^n(z-z_1)(z-z_2)},z=0\bigg)\bigg]\\
&=&\frac{\pi}{a}\bigg[\frac{z_1^{2n}+1}{z_1^n(z_1-z_2)}+\frac{z_1^n-z_2^n}{z_1^nz_2^n(z_1-z_2)}\bigg]\\
&=&\frac{\pi}{a}\frac{1+z_1^nz_2^n}{(z_1-z_2)z_2^n}\\
&=&\frac{\pi}{\sqrt{1-a^2}z_2^n}
\end{eqnarray}
where
$$z_1=\frac{-1+\sqrt{1-a^2}}{a},z_2=\frac{-1-\sqrt{1-a^2}}{a}. $$
A: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

$\ds{\int_{0}^{\pi}{\cos\pars{n\theta} \over 1 + a\cos\pars{\theta}}\,\dd\theta =
{\pi \over \root{1-a^2}}\pars{\root{1 - a^{2}} -1 \over a}^{\verts{n}}:\
{\large ?}.\qquad
\left\{\substack{\ds{\verts{a} \in \pars{0,1}}
\\[3mm]
\ds{n \in \mathbb{Z}}}\right.}$.

\begin{align}
&\int_{0}^{\pi}{\cos\pars{n\theta} \over 1 + a\cos\pars{\theta}}\,\dd\theta =
\Re\int_{0}^{\pi}
{\expo{\ic\verts{n}\theta} \over 1 + a\cos\pars{\theta}}\,\dd\theta =
\Re\int_{\substack{z\ =\ \exp\pars{\ic\theta}
\\[0.5mm]
\theta\ \in\ \pars{0,\pi} }}
{z^{\verts{n}} \over 1 + a\pars{z + 1/z}/2}\,{\dd z \over \ic z}
\\[5mm] = &\
2\,\Im\int_{\substack{z\ =\ \exp\pars{\ic\theta}
\\[0.5mm]
\theta\ \in\ \pars{0,\pi} }}
{z^{\verts{n}} \over az^{2} + 2z + a}\,\dd z =
2\,\Im\int_{\substack{z\ =\ \exp\pars{\ic\theta}
\\[0.5mm]
\theta\ \in\ \pars{0,\pi} }}
{z^{\verts{n}} \over a\pars{z - r_{-}}\pars{z - r_{+}}}\,\dd z
\\[5mm]
&\mbox{where}\quad r_{\pm} \equiv {-1 \pm \root{1 - a^{2}} \over a} \in \mathbb{R}\,,\quad
\verts{a} \in \pars{0,1}\,;\qquad
\left\{\substack{\ds{\verts{r_{+}} < 1}\\[3mm]
\ds{\verts{r_{-}} > 1}}\right.
\end{align}

Then,
\begin{align}
&\int_{0}^{\pi}{\cos\pars{n\theta} \over 1 + a\cos\pars{\theta}}\,\dd\theta
\\[5mm] = &\
\lim_{\epsilon \to 0^{+}}\left[%
-\,{2 \over a}\,\Im\int_{-1}^{r_{+} - \epsilon}
{x^{\verts{n}} \over \pars{x - r_{-}}\pars{x - r_{+}}}\,\dd x -
{2 \over a}\,\Im\int_{\pi}^{0}
{r_{+}^{\verts{n}} \over \pars{r_{+} - r_{-}}\epsilon\expo{\ic\theta}}\,
\epsilon\expo{\ic\theta}\ic\,\dd\theta
\right.
\\[2mm] &\ \left.%
\phantom{\lim_{\epsilon \to 0^{+}}\left[\,\right.}
-\,{2 \over a}\,\Im\int_{r_{+} + \epsilon}^{1}
{x^{\verts{n}} \over \pars{x - r_{-}}\pars{x - r_{+}}}\,\dd x
\right]\label{1}\tag{1}
\\[1cm] = &\
-\,{2 \over a}\,{-\pi \over r_{+} - r_{-}}\,r_{+}^{\verts{n}} =
\bbx{{\pi \over \root{1-a^2}}\pars{\root{1 - a^{2}} -1 \over a}^{\verts{n}}}
\end{align}


The imaginary part of the first and the third integrals, in \eqref{1}, vanish out because their integrand just 'take' real values.

