Evaluating $\int_{0}^{\pi} \frac{\cos(nx)}{(p+\cos(x))^2+q^2}\ \mathrm dx$ 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.
 A: 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 
A: Have you tried checking WolframAlpha/Mathematica?
For example, here is what happens when $n = 1$: antiderivative
If you now change $n$ to $2, 3, 4, 5$ etc., you will see various antiderivatives come up.
They all appear to be of the form $f_{n}(p,q)H - g_{n}(p,x)$, where the antiderivative showing up is not quite as horrible as it looks at first. In particular, the $H$ looks like a mess with the denominator and some nasty inverse tangent expression, but it appears to be the same $H$ occurring as $n$ varies.
Also, WolframAlpha is not showing the full cancellation that occurs in the numerator.
My suggestion would be to write out the $f_{n}(p,q)$ and $g_{n}(p,x)$ in simplest form as $n$ varies, see if a pattern makes itself apparent, and then work backwards.
Note: If anyone should feel up to listing the first few, feel free to edit this answer or submit them in a separate response. (I've made this community wiki because it doesn't actually give an answer....)
A: $\newcommand{\+}{^{\dagger}}%
 \newcommand{\angles}[1]{\left\langle #1 \right\rangle}%
 \newcommand{\braces}[1]{\left\lbrace #1 \right\rbrace}%
 \newcommand{\bracks}[1]{\left\lbrack #1 \right\rbrack}%
 \newcommand{\dd}{{\rm d}}%
 \newcommand{\isdiv}{\,\left.\right\vert\,}%
 \newcommand{\ds}[1]{\displaystyle{#1}}%
 \newcommand{\equalby}[1]{{#1 \atop {= \atop \vphantom{\huge A}}}}%
 \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}%
 \newcommand{\fermi}{\,{\rm f}}%
 \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}%
 \newcommand{\ic}{{\rm i}}%
 \newcommand{\imp}{\Longrightarrow}%
 \newcommand{\ket}[1]{\left\vert #1\right\rangle}%
 \newcommand{\pars}[1]{\left( #1 \right)}%
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\pp}{{\cal P}}%
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,#2\,}\,}%
 \newcommand{\sech}{\,{\rm sech}}%
 \newcommand{\sgn}{\,{\rm sgn}}%
 \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}}
 \newcommand{\ul}[1]{\underline{#1}}%
 \newcommand{\verts}[1]{\left\vert #1 \right\vert}%
 \newcommand{\yy}{\Longleftrightarrow}$
$\ds{\int_{0}^{\pi}{\cos\pars{nx} \over \bracks{\,p + \cos\pars{x}}^{2} + q^{2}}
\,\dd x:\ {\large ?}}$.

Notice that
\begin{align}
&\int_{0}^{\pi}
{\cos\pars{nx} \over \bracks{\,p + \cos\pars{x}}^{2} + q^{2}}\,\dd x
=
\int_{0}^{\pi}
{\cos\pars{nx}
 \over
 \bracks{\cos\pars{x} - \pars{-p - \verts{q}\ic}}
 \bracks{\cos\pars{x} - \pars{-p + \verts{q}\ic}}}\,\dd x
\end{align}
We set the definition:
\begin{align}
{\cal I}_{n}\pars{\xi}
&\equiv
\int_{0}^{\pi}{\cos\pars{nx} \over \bracks{\,p + \cos\pars{x}}^{2} + q^{2}}
\,\dd x
=
\int_{0}^{\pi}
{\cos\pars{nx} \over \bracks{\cos\pars{x} - \xi}\bracks{\cos\pars{x} - \xi^{*}}}
\,\dd x
\\[1mm]&\mbox{where}\quad\xi \equiv -p - \verts{q}\ic
\end{align}
The integral diverges whenever $\verts{p} \leq 1$ and $q = 0$
$\pars{~\mbox{it means}\ \xi \in {\mathbb R}\ \mbox{and}\ \verts{\xi} \leq 1~}$. Hereafter, we assume
$$
\verts{p} > 1\quad\mbox{or}\quad q \not= 0
$$

Also,
\begin{align}
{\cal I}_{n}\pars{\xi}
&\equiv
\int_{0}^{\pi}{\cos\pars{nx} \over \xi - \xi^{*}}\,
\bracks{{1 \over \cos\pars{x} - \xi} - {1 \over \cos\pars{x} - \xi^{*}}}\,\dd x
=
\int_{0}^{\pi}{\cos\pars{nx} \over 2\ic\,\Im\xi}\,2\ic
\Im\bracks{{1 \over \cos\pars{x} - \xi}}\,\dd x
\\[3mm]&=
-\,{1 \over \verts{q}}\,
\Im\int_{0}^{\pi}{\cos\pars{nx} \over \cos\pars{x} - \xi}\,\dd x
=
-\,{1 \over 2\verts{q}}\,
\Im\int_{0}^{\pi}{\expo{\ic nx} + \expo{-\ic nx}\over \cos\pars{x} - \xi}\,\dd x
\\[3mm]&=
-\,{1 \over 2\verts{q}}\,\Im\bracks{%
\int_{0}^{\pi}{\expo{\ic nx} \over \cos\pars{x} - \xi}\,\dd x
-
\int_{0}^{-\pi}{\expo{\ic nx} \over \cos\pars{x} - \xi}\,\dd x}
=
-\,{1 \over 2\verts{q}}\,\Im
\int_{-\pi}^{\pi}{\expo{\ic nx} \over \cos\pars{x} - \xi}\,\dd x
\\[3mm]&=
-\,{1 \over 2\verts{q}}\,\Im
\oint_{\verts{z} = 1}{z^{n} \over \pars{z^{2} + 1}/\pars{2z} - \xi}
\,\pars{-\ic\,{\dd z \over z}}
=
{1 \over \verts{q}}\,\Re
\oint_{\verts{z} = 1}{z^{n} \over z^{2} - 2\xi z + 1}\,\dd z
\\&\mbox{We just need to consider the case}\ n \geq 0\ \mbox{since}\
{\cal I}_{-n}\pars{\xi} = {\cal I}_{n}\pars{\xi}.
\end{align}

The roots $z_{\pm}$ of $z^{2} - 2\xi z + 1 = 0$ are given by
$z_{\pm} = \xi \pm \root{\xi^{2} - 1}$. Since $z_{+}z_{-} = 1$, one of the roots is 'inside' the integration contour $\pars{~\mbox{the case}\ \verts{z_{\pm}} = 1\
\mbox{is excluded when}\ \verts{p} > 1\ \mbox{or}\ q = 0~}$.



*
*
If $\verts{z_{-\sigma}} > 1$, then $\verts{z_{\sigma}} < 1$
$\pars{~\mbox{with}\ \sigma = \pm~}$ and ${\cal I}_{n}\pars{\xi}$ is reduced to:
\begin{align}
{\cal I}_{n}\pars{\xi}
&=
{1 \over \verts{q}}\,\Re\pars{2\pi\ic\,
{z^{\verts{n}} \over z - z_{-\sigma}}}_{z\ =\ z_{\sigma}}
=
-\,{2\pi \over \verts{q}}\,\Im\pars{%
{z_{\sigma}^{\verts{n}} \over z_{\sigma} - z_{-\sigma}}}
\\[3mm]&=
-\,{2\pi \over \verts{q}}\,\Im\bracks{%
{\pars{\xi + \sigma\root{\xi^{2} - 1}}}^{\verts{n}} \over 2\sigma\root{\xi^{2} - 1}}
=
-\,{\sigma\,\pi \over \verts{q}}\,\Im\bracks{%
{\pars{\xi + \sigma\root{\xi^{2} - 1}}}^{\verts{n}} \over \root{\xi^{2} - 1}}
\\[1mm]&\mbox{where we choose}\quad \sigma\ \ni\
\verts{\,\xi + \sigma\root{\xi^{2} - 1}} < 1\,,\qquad\xi = -p - \verts{q}\ic
\end{align}

*When $\verts{p} > 1$ and $q \to 0$,
$\left.{\cal I}_{n}\pars{\xi}\right\vert_{\verts{p} > 1 \atop q = 0} = {\cal I}\pars{-p}$ is evaluated as:
\begin{align}
&\left.{\cal I}_{n}\pars{\xi}\right\vert_{\verts{p} > 1 \atop q = 0}
=
-\sigma\,\pi\,\lim_{\verts{q} \to 0}\partiald{}{\verts{q}}\Im\bracks{%
{\pars{\xi + \sigma\root{\xi^{2} - 1}}}^{\verts{n}} \over \root{\xi^{2} - 1}}
\\[3mm]&=
\sigma\,\pi\,\lim_{\xi \to -p}\Re\partiald{}{\xi}\bracks{%
{\pars{\xi + \sigma\root{\xi^{2} - 1}}}^{\verts{n}} \over \root{\xi^{2} - 1}}
\\[3mm]&=
-\sigma\,\pi\,\partiald{}{p}\bracks{%
{\pars{-p + \sigma\root{p^{2} - 1}}}^{\verts{n}} \over \root{p^{2} - 1}}
\end{align}
When $p < -1$, we choose $\sigma = -$ and with $p > 1$ we choose $\sigma = +$ such that:
\begin{align}
&\left.{\cal I}_{n}\pars{\xi}\right\vert_{\verts{p} > 1 \atop q = 0}
=
-\pi\,\epsilon_{n}\partiald{}{\verts{p}}\bracks{%
{\pars{\verts{p} - \root{\verts{p}^{2} - 1}}}^{\verts{n}}
 \over \root{\verts{p}^{2} - 1}}
\\&\mbox{where}\
\epsilon_{n} = \left\lbrace%
\begin{array}{ccl}
1 & \mbox{when} & p < -1
\\
\pars{-1}^{\verts{n}} & \mbox{when} & p > 1
\end{array}\right.
\end{align} 


A: The explicit formula can be found in a long and winding way. First, use formula 1.331.3 from Gradshtein & Ryzhik to express $\cos(nx)$ as the weighted sum of powers $\cos(x)^k*\sin(x)^{n-k}.$ Then the integral under consideration equals the sum of integrals. Some of those seem to be zeros. The others should be extended to the range of integration $[0,2\pi]$ (This doubles  the values.). The next step consists in rewriting $$\int_{0}^{2\pi} \frac{\cos(x)^k\sin(x)^{n-k}}{(p+\cos(x))^2+q^2}\ \mathrm dx  $$ as the contour integral over the unit circle $\{z:|z|=1\}$ counterclockwise. That contour integral can be calculated by residues (the parameters must to be taken into account). Being a normal person, I don't find any motivation to do the job. Good luck!
