Show that $\int_0^{\pi}\frac {\cos {n \theta}}{1-2r\cos \theta+r^2}\, \mathrm d \theta = \frac {\pi r^n}{1-r^2}$ I am trying to calculate $$I=\int_0^{\pi}\frac {\cos {n \theta}}{1-2r\cos \theta+r^2}\, \mathrm d \theta$$
where $r\in(0,1)$
I tried substituting $u = e^{2 i \theta}$ and using the Cauchy integral formula:
\begin{align}I&=\Re \int_{|u|=1}\frac {u^{n/2}}{(r-u^{1/2})(r-u^{-1/2})}\frac 1 {2iu} \,\mathrm d u \\&=\Re \frac {i} {2}\int_{|u|=1} \frac {u^{n/2 - 1}(r+u^{1/2})}{(u-r^2)(r-u^{-1/2})}\, \mathrm d u \\ &=\frac {2 \pi r^{n}}{1-r^2} \end{align}
but the correct answer is $\dfrac {\pi r^n}{1-r^2}$
Where did I make a mistake?

In the integrand, you have
$$
\begin{align}
\frac {u^{n/2 - 1}\left(r+u^{1/2}\right)}{\left(u-r^2\right)\left(r-u^{-1/2}\right)}
&=\frac{ru^{n/2-1}}{\left(u-r^2\right)\left(r-u^{-1/2}\right)}
+\frac{u^{(n-1)/2}}{\left(u-r^2\right)\left(r-u^{-1/2}\right)}
\end{align}
$$
Unfortunately, $u^{-1/2}$ is not well defined on the unit circle. Similarly, if $n$ is odd, $u^{n/2-1}$ is not well defined on the unit circle, and if $n$ is even, $u^{(n-1)/2}$ has the same problem.

Here is how I would do it to avoid this problem:
$$
\begin{align}
\int_0^\pi\frac{\cos(n\theta)}{1-2r\cos(\theta)+r^2}\,\mathrm{d}\theta
&=\frac12\int_{-\pi}^\pi\frac{\cos(n\theta)+i\sin(n\theta)}{1-2r\cos(\theta)+r^2}\,\mathrm{d}\theta\tag{1}\\
&=\frac12\oint_{\left|z\right|=r}\frac{z^n}{(z-r)\left(\frac1z-r\right)}\frac{\mathrm{d}z}{iz}\tag{2}\\
&=\frac1{2i}\oint_{\left|z\right|=1}\frac{z^n}{(z-r)(1-rz)}\,\mathrm{d}z\tag{3}\\
&=\frac1{2i}\frac{2\pi ir^n}{1-r^2}\tag{4}\\[3pt]
&=\frac{\pi r^n}{1-r^2}\tag{5}
\end{align}
$$
Explanation:
$(1)$: $\cos(x)$ is even and $\sin(x)$ is odd
$(2)$: $z=e^{i\theta}$ and De Moivre's Formula
$(3)$: algebra
$(4)$: $z=r$ is the only singularity inside the unit circle
$(5)$: algebra
