# Solving $\int_0^\pi \frac{\cos(kn)}{1+k^2}dk$

I want to solve the integral $$\int_0^\pi \frac{\cos(kn)}{1+k^2}dk$$.

In my attempt, I tried do Integration by Parts twice, but this didn't get me anywhere. Then, I thought about using the Residue theorem, but to do that I needed the limits of integration to be $$-\infty$$ to $$\infty$$.

What else can I do to analytically solve this?

$$\int\limits_{0}^{\pi}\frac{\cos(kn)}{1+k^2}dk$$

\begin{align} \int\limits_{0}^{\pi}\frac{\cos(kn)}{1+k^2}dk &= \int\limits_{0}^{\pi}\frac{\cos(kn)}{(k+i)(k-i)}dk\\ &=\int\limits_{0}^{\pi}\frac i 2\left(\frac{\cos (k n)}{k+i}-\frac{\cos (k n)}{k-i}\right)dk\\ &=\frac{i}{2}\int\limits_{0}^{\pi}\frac{\cos(kn)}{k+i}dk-\frac{i}{2}\int\limits_{0}^{\pi}\frac{\cos(kn)}{k-i}dk \end{align} Consider the first integral term and perform the change of variables $$k = x - i$$.

\begin{align} \frac{i}{2}\int\limits_{0}^{\pi}\frac{\cos (k n)}{k+i} &= \frac{i}{2}\int\limits_{i}^{\pi+i} \frac{\cos (n (x-i))}{x} dx\\ &= \frac{i}{2}\int\limits_i^{\pi+i} \cosh (n) \frac {\cos (n x)}{x}+i\sinh (n) \frac{\sin (n x)} {x} dx\\ &=n\frac{i}{2}\cosh(n)\int\limits_{i}^{\pi+i}\frac{\cos(nx)}{nx}dx - n\frac{1}{2}\sinh(n)\int\limits_{i}^{\pi+i} \frac{\sin(nx)}{nx}dx \end{align}

Change of variables: $$t = nx$$:

\begin{align} &=n\frac{i}{2}\cosh(n)\int\limits_{n i}^{n(\pi + i)} \frac{\cos(t)}{t}dt - n\frac{1}{2}\sinh(n)\int\limits_{ni}^{n(\pi+i)}\frac{\sinh(t)}{t}dt\\ &= n\frac{i}{2}\cosh(n) \left ( Ci(n(\pi+i) - Ci(ni)) \right ) - n\frac{1}{2}\sinh(n)\left( Si(n(\pi+i)) - Si(ni)\right ) \end{align}

Likewise, the second integral term of $$(3)$$ becomes:

$$$$\begin{split} &\frac{i}{2}\int\limits_{0}^{\pi}\frac{\cos(kn)}{k-i}dk \\ &=n\frac{i}{2}\cosh(n) \left ( Ci(n(\pi-i) - Ci(-ni)) \right ) + n\frac{1}{2}\sinh(n)\left( Si(n(\pi-i)) - Si(-ni)\right ) \end{split}$$$$

Therefore, $$(3) = (8) - (9)$$.

• Let $F(n)$ be the value of the integral, well, we may let $n$ live in $\Bbb R$. Note that $F$ is smooth and $F''=-F$. We know $F(0)$. We need only $F'(0)$... Commented Sep 7, 2023 at 22:52
• I am sure the limits are correct, but I think my original attempt using IBP was wrong. I am rewriting it now Commented Sep 7, 2023 at 23:20
• Via integration by parts twice, I get $\cos(\pi n)\arctan(\pi)$ Commented Sep 7, 2023 at 23:41
• This cannot be correct. Take $n = 1/2$, $\cos(k/2) > 0$ for $0 < k < \pi$ so the integral is positive, but you get $0$. Commented Sep 7, 2023 at 23:50
• @dan_fulea We do not get $F'' = -F$. We get $F'' = F - \sin(\pi n)/n$ Commented Sep 8, 2023 at 1:08

The problem is not too difficult if you start with$$\frac{\cos(kn)}{1+k^2}=\frac{\cos(kn)}{(k+i)(k-i)}=\frac i 2\left(\frac{\cos (k n)}{k+i}-\frac{\cos (k n)}{k-i}\right)$$

Consider the first one and let $$k=x-i$$ $$\frac{\cos (k n)}{k+i}=\frac{\cos (n (x-i))}{x}$$ Expand the cosine $$\frac{\cos (k n)}{k+i}=n\cosh (n) \frac {\cos (n x)} {nx}+in \sinh (n) \frac{\sin (n x)} {nx}$$ Another obvious change of variable will give the antiderivative in terms of sine and cosine integrals.

Finish, go back to $$x$$ and then to $$k$$ and apply the bounds.

I am sure that you can take it from here.

Yes, there was a big error in my hint, here is the way how the idea is made to work. We define the function $$F(n)$$, so that its real part is the value of the integral to be computed. Here $$n$$ is a real or even complex parameter. By dominated convergence, inductively, the derivatives of $$F$$ exist and are continuous, so $$F$$ is a function of class $$\mathcal C^\infty$$. (We need only the class $$\mathcal C^2$$.)

We obtain a second order differential equation for $$F$$, which we then solve. Let $$J$$ be the interval $$[0,\pi]$$ for short. \begin{aligned} F(n) &= \int_J(\cos(kn)+i\sin(kn))\cdot \frac 1{1+k^2}\; dk = \int_J\frac {e^{ikn}}{1+k^2}\; dk \ ,\qquad\text{ the definition of F },\\ F'(n) &= \int_J\frac {ik\; e^{ikn}}{1+k^2}\; dk\ ,\\ F''(n) & = \int_J\frac {(ik)^2\; e^{ikn}}{1+k^2}\; dk = \int_J\frac{(1-(1+k^2))\; e^{ikn}}{1+k^2}\; dk \\ &=F(n)-\int_J\; e^{ikn}\; dk = F(n)-\frac 1{in}(e^{i\pi n}-1)\ . \end{aligned} This is an inhomogeneous differential equation in $$F$$, second order, the general solution of the associated homogeneous equation $$F''=F$$ is $$F(n)=Se^n +T e^{-n}$$, with $$S,T$$ constants. We search for a solution of the same shape, but with $$S,T$$ functions of $$n$$, so that they satisfy $$S'e^n +T' e^{-n}=0$$. We thus have: \begin{aligned} F(n) &= Se^n+ Te^{-n}\ ,\\ 0 &= S'e^n+ T'e^{-n}\ ,\\ F'(n)&=Se^n-Te^{-n}\ ,\\ -\frac 1{in}(e^{i\pi n}-1) &=F''(n)-F(n)=S'e^n-T'e^{-n}\ , \\[3mm] &\qquad\text{ and we extract S' and T':} \\[3mm] S'(n) &=-\frac 1{2in}(e^{i\pi n-n}-e^{-n})\ ,\\ T'(n) &=+\frac 1{2in}(e^{i\pi n+n}-e^{+n})\ , \\[3mm] &\qquad\text{ we integrate and the elliptic integral Ei appears:} \\[3mm] S(n) &= S_0 -\frac 1{2i}\operatorname{Ei}(i\pi n-n) + \frac 1{2i}\operatorname{Ei}(-n)\ ,\\ T(n) &= T_0 +\frac 1{2i}\operatorname{Ei}(i\pi n+n) - \frac 1{2i}\operatorname{Ei}(+n)\ , \\[3mm] &\qquad\text{ where S_0=S(0) and T_0=T(0) are extracted from boundary conditions:} \\[3mm] S_0+T_0 &= F(0)=\arctan \pi\ ,\\ S_0 - T_0 &= F'(0)=\frac i2\log(1+\pi^2)\ . \end{aligned} We need only the real part of $$F$$, thus use only the real part of $$S,T$$, and of $$S_0,T_0$$. This gives, by plugging in $$S(n),T(n)$$ obtained above: \begin{aligned} \int_J\frac {\cos(kn)}{1+k^2}\; dk &= \Re F(n) \\ &= e^n\Re S(n) +e^{-n}\Re T(n) \\ &= \frac 12 e^n\left(\arctan \pi -\Im \operatorname{Ei}(i\pi n-n)\right) + \frac 12 e^{-n}\left(\arctan \pi +\Im \operatorname{Ei}(i\pi n+n)\right) \\ &= \bbox[yellow]{\quad \arctan \pi\cdot\cosh n +\frac 12 \Im \operatorname{Ein}(n-i\pi n)\;e^n - \frac 12 \Im\operatorname{Ein}(-n-i\pi n)\;e^{-n}\quad} \ . \end{aligned} For the definition of the exponential integrals Ei and Ein see: https://en.wikipedia.org/wiki/Exponential_integral

I want the final formula in terms of Ein, since this is an analytic function with a rapidly converging defining series, Ein$$\displaystyle(z)=\int_0^z(1-e^{-t})\frac{dt}t=\sum_{k\ge 1}(-1)^{k-1}\frac 1{k\cdot k!}z^k$$.

We do not have a solution in terms of (more) "elementary functions".

Using sage, here is a numerical check for some few integer values of $$n$$:

var('k');
C = ComplexField(300)

def myint(n):
return C(integral( cos(k*n) /( 1 + k^2), k, 0, pi, hold=True ))

def myEin(z, N=1000):
return sum( [(-1)^(k+1)*C(z)^k / k / factorial(k) for k in [1..N]] )

def myformula(n):
return C( cosh(n)*atan(pi)
+ exp(+n)*imag_part(myEin(+n - i*pi*n))/2
- exp(-n)*imag_part(myEin(-n - i*pi*n))/2 )

for n in [1..5]:
print(f"n = {n}")
print(f"The integral of cos({n}k) / (1 + k²) on J is approximatively:")
print(f"\t{myint(n)}")
print(f"The formula for the computed integral gives approximatively:")
print(f"\t{myformula(n)}")


This gives:

n = 1
The integral of cos(1k) / (1 + k²) on J is approximatively:
0.610868747184194482713337492896243929862976074218750000000000000000000000000000000000000000
The formula for the computed integral gives approximatively:
0.610868747184194416544125714452669291251337530321233820460971978842210929983350135651189311
n = 2
The integral of cos(2k) / (1 + k²) on J is approximatively:
0.201416860520674451118239289826306048780679702758789062500000000000000000000000000000000000
The formula for the computed integral gives approximatively:
0.201416860520674509589374702154530278923421249087903359932749383575147501740722898491576047
n = 3
The integral of cos(3k) / (1 + k²) on J is approximatively:
0.0836187169914809347304185394023079425096511840820312500000000000000000000000000000000000000
The formula for the computed integral gives approximatively:
0.0836187169914809672154886010910836437157471005914446871938625977167342928982700877244376710
n = 4
The integral of cos(4k) / (1 + k²) on J is approximatively:
0.0256146196980378236529585223024696460925042629241943359375000000000000000000000000000000000
The formula for the computed integral gives approximatively:
0.0256146196980378253348190247682598343647333105316442349666924193360125520079274189471180402
n = 5
The integral of cos(5k) / (1 + k²) on J is approximatively:
0.0126397196249749318303079448355674685444682836532592773437500000000000000000000000000000000
The formula for the computed integral gives approximatively:
0.0126397196249748971603477585878210191331883527662081413506614588695357613254482363328750369


A good numerical check!

• Thank you Dan, this looks wonderful. I wonder if this is equivalent to the approach by Claude. In that approach, in my attempt my final answer is in terms of the Cosine and Sine integrals. What do you think? Also, this turned out to be a much more interesting problem than I realized. I've learned a lot! Commented Sep 11, 2023 at 15:07