Does an analytical form exist for the following integral I have an integral
$$f(n,a)=\int_0^{2\pi}\mathop{\mathrm{d}x}\frac{\cos(nx)\cos^2x}{1-a\cos^2x},$$
where $n$ is an even integer and $0<a<1$ is a real number. Does an analytical form exist for this integral?
Edit: Here is a partial answer that I discovered:
$$\int_0^{2\pi}\mathop{\mathrm{d}x}\frac{\cos(nx)}{1-a\cos^2x}=-\frac{2\pi}{\sqrt{1-a}}\left(\frac{1-\sqrt{1-a}}{\sqrt{a}}\right)^n,\qquad a<1, n>0.$$
$n$ is even.

The answer is 
$$f(n, a) = \frac{2\pi}{a\sqrt{1-a}}\left(\frac{1-\sqrt{1-a}}{\sqrt{a}}\right)^n \tag{1}$$
for $0 < a < 1$ and $n > 0$ an even integer; we also have $$f(0, a) = \frac{2\pi}{a \sqrt{1-a}} - \frac{2\pi}{a}$$
which (mysteriously) happens to equal what $\sqrt{a}f(1, a)$ would be if we put $n=1$ in formula $(1)$.
First, consider the related integral $$g(n, a) = \int_0^{2\pi}\frac{\cos nx}{1-a\cos^2 x}\,dx$$ 
which is useful because 
$$g(n, a) - a f(n, a) = \int_0^{2\pi} \cos nx\,dx = \begin{cases}2\pi, & n = 0 \\ 0, & n \ne 0.\end{cases} \tag{2}$$
Express $g$ as a contour integral via $z = e^{ix}$, 
noting that $\cos nx = \frac{1}{2}(z^n + z^{-n})$ and $dz = iz\,dx$. 
After some simplification, we get:
$$g(n,a) = 2i \int_{|z|=1} \frac{z^n + z^{-n}}{az^4 + 2(a-2)z^2 + a} z\,dz. \tag{3}$$
The polynomial $Q_a(z)$ in the denominator has four roots, 
but since it's biquadratic they are easy to determine explicitly. 
The quadratic formula gives 
$$\pm\sqrt{\frac{2}{a}\left(1\pm\sqrt{1-a}\right)-1}$$
and a stroke of luck denesting radicals gives 
$$\pm\frac{1\pm\sqrt{1-a}}{\sqrt{a}}.$$
It isn't hard to see (either by elementary means or Rouché's theorem) that these four roots are real and distinct, and precisely two of them lie within the unit disk: $\alpha = (1-\sqrt{1-a})/\sqrt{a}$ and $-\alpha$. Let's call the other pair $\pm\beta$. From the coefficients of $Q_a$ we deduce that $\alpha^2 \beta^2 = 1$.
Write $P_n(z) = z(z^n + z^{-n})$. The integrand in $(3)$ has simple poles at $\alpha$ and $-\alpha$ with matching residues there: $$\frac{P(\pm\alpha)}{Q_a'(\pm\alpha)} = -\frac{1}{8\sqrt{1-a}}\left(\alpha^n + \beta^n\right).\tag{4}$$
For $n > 0$ there is also a pole at the origin of order $n-1$, and this is painful to deal with.  
Here's what I did. (This may not be the best approach.) 
Write
$$\frac{1}{aw^2 + 2(a-2)w + a} = \sum_{k=0}^\infty c_k w^k$$
as a (formal) power series.
Since this is a rational function, 
the coefficients $c_k$ satisfy a recurrence with a closed form involving powers of the roots $\alpha^2$ and $\beta^2$. Cross multiplying, we see that
$$c_0 = \frac{1}{a},\quad c_1 = \frac{2(2-a)}{a^2},\quad c_k = a c_1 c_{k-1} - c_{k-2}.$$
Knowing that the general solution is of the form $c_k = \lambda (\alpha^2)^k + \mu (\beta^2)^k$, plug in the initial conditions and solve: 
$$\lambda = \frac{a-2+2\sqrt{1-a}}{4a\sqrt{1-a}}$$
$$\mu = \frac{2-a+2\sqrt{1-a}}{4a\sqrt{1-a}} = \frac{1}{a} - \lambda.$$
We seek the coefficient of $z^{-1}$ in the integrand, which looks like
$$z(z^n + z^{-n})\sum_{k=0}^\infty c_k z^{2k}$$
around the origin, so we want $2k + 1 - n = -1$, or $k = \frac{n}{2}-1$. 
Thus, the residue is $$\lambda\beta^2\alpha^n + \mu\alpha^2\beta^n\tag{5}$$
and combining $(5)$ with (the double contribution of) $(4)$ we obtain, by the residue theorem, 
$$g(n, a) = 4\pi\left\{\left(\frac{1}{4\sqrt{1-a}}-\lambda\beta^2\right)\alpha^n + \left(\frac{1}{4\sqrt{1-a}}-\mu\alpha^2\right)\beta^n\right\}.$$
Miraculously, the coefficient of $\beta^n$ vanishes. 
Simplifying, we conclude that 
$$g(n, a) = \frac{2\pi}{\sqrt{1-a}}\left(\frac{1-\sqrt{1-a}}{\sqrt{a}}\right)^n$$
where, of course, 
the sign of the root $\alpha$ does not matter as $n$ is even. This in $(2)$ yields $(1)$.

Update. A much simpler way to evaluate $g(n, a)$ has been brought to my attention. 
$$g(n, a) = \int_0^{2\pi} \frac{e^{ix}}{1-a\cos^2 x}\,dx$$
because the imaginary part (the sine integral) vanishes (the integrand is odd); 
now let $z = e^{ix}$. The integral becomes
$$4i \int_{|z|=1} \frac{z^{n+1}}{Q_a(z)}\,dz$$ 
which equals
$$(2\pi i)(4 i)\frac{2\alpha^n}{-8\sqrt{1-a}} = \frac{2\pi \alpha^n}{\sqrt{1-a}}$$
by the residue theorem.
