TITLE: Evaluating $\int_{0}^{\pi} \frac{\cos(nx)}{(p+\cos(x))^2+q^2}\ \mathrm dx$
QUESTION [5 upvotes]: I have a formula in my research, but have no idea how to get the explicit formula.
$$\int_{0}^{\pi} \frac{\cos(nx)}{(p+\cos(x))^2+q^2}\ \mathrm dx$$
where n is an integer.

REPLY [3 votes]: The following observation may be helpful: Let $\sigma, \xi \in \Bbb{R}$ satisfy the following system of equations
\begin{align*}
p &= \cosh \sigma \cos \xi, \\
q &= -\sinh \sigma \sin \xi.
\end{align*}
(Or equivalently, $\cosh(\sigma+i\xi) = p + iq$.) Then
\begin{align*}
I_{n} := \int_{0}^{\pi} \frac{\cos nx}{(p + \cos x)^{2} + q^{2}} \, dx
&= \frac{1}{2} \int_{-\pi}^{\pi} \frac{e^{inx}}{(p + \cos x)^{2} + q^{2}} \, dx \\
&= -2i \int_{|z| = 1} \frac{z^{n+1}}{P(z)} \, dz,
\end{align*}
where $P(z)$ is the quartic polynomial defined by
\begin{align*}
P(z)
&= 1 + 4 p z + (2 + 4 p^2 + 4 q^2) z^2 + 4 p z^3 + z^4 \\
&= (z + e^{\sigma+i\xi})(z + e^{\sigma-i\xi})(z + e^{-\sigma+i\xi})(z + e^{-\sigma-i\xi}).
\end{align*}
So a simple residue calculation shows that
$$ I_{n} = 4\pi \left\{ \frac{ (-e^{-|\sigma|+i\xi})^{n+1} }{P'(-e^{-|\sigma|+i\xi})} + \frac{ (-e^{-|\sigma|-i\xi})^{n+1} }{P'(-e^{-|\sigma|-i\xi})} \right\}, $$
which equals for $\sigma > 0$
$$ I_{n} = (-1)^n \frac{\pi e^{-n \sigma}}{\sinh \sigma \sin \xi} \cdot \frac{\sinh\sigma \cos \xi \sin (n \xi) + \cosh \sigma \sin \xi \cos (n \xi)}{\sinh^{2} \sigma \cos^{2} \xi + \cosh^{2} \sigma \sin^{2} \xi}. $$
You can simulate this using the following Mathematica code:
  {\[Sigma], \[Xi], n} = {1, 3, 6};
  P[z_] := 1 + 2 z^2 + z^4 + 4 z Cos[\[Xi]] Cosh[\[Sigma]] + 
     4 z^3 Cos[\[Xi]] Cosh[\[Sigma]] + 
     4 z^2 Cos[\[Xi]]^2 Cosh[\[Sigma]]^2 + 
     4 z^2 Sin[\[Xi]]^2 Sinh[\[Sigma]]^2;
  NIntegrate[
   Cos[n x]/((Cosh[\[Sigma]] Cos[\[Xi]] + 
      Cos[x])^2 + (Sinh[\[Sigma]] Sin[\[Xi]])^2), {x, 0, Pi}, 
   WorkingPrecision -> 50]
  N[4 Pi ((z^(n + 1)/D[P[z], z] /. 
        z -> (-Exp[-Abs[\[Sigma]] + I \[Xi]])) + (z^(n + 1)/
        D[P[z], z] /. z -> (-Exp[-Abs[\[Sigma]] - I \[Xi]]))), 50]
  Clear[\[Sigma], \[Xi], n, P];

I haven't think on how to reduce this to a simple formula involving $p$ and $q$, but you may work further to reduce