Integral $ \int_{-\infty}^{\infty}\frac{x^{2}}{\cosh\left(x\right)}\,{\rm d}x $

Integrals of the form

$$\int_{-\infty}^\infty \frac{p(x)}{\cosh x}\,dx,$$

where $p$ is a polynomial can be evaluated by shifting the contour of integration to a line $\operatorname{Im} z \equiv c$. We first check that the integrals over the vertical segments connecting the two lines tend to $0$ as the real part tends to $\pm\infty$:

$$\lvert \cosh (x+iy)\rvert^2 = \lvert \cosh x\cos y + i \sinh x\sin y\rvert^2 = \sinh^2 x + \cos^2 y,$$

so the integrand decays exponentially and

$$\left\lvert \int_{R}^{R + ic} \frac{p(z)}{\cosh z}\,dz\right\rvert \leqslant \frac{K\,c}{\sinh R}\left(R^2+c^2\right)^{\deg p/2} \xrightarrow{R\to \pm\infty} 0.$$

Since $\cosh \left(z+\pi i\right) = -\cosh z$, and the only singularity of the integrand between $\mathbb{R}$ and $\mathbb{R}+\pi i$ is a simple pole at $\frac{\pi i}{2}$ (unless $p$ has a zero there, but then we can regard it as a simple pole with residue $0$) with the residue

$$\operatorname{Res}\left(\frac{p(z)}{\cosh z};\, \frac{\pi i}{2}\right) = \frac{p\left(\frac{\pi i}{2}\right)}{\cosh' \frac{\pi i}{2}} = \frac{p\left(\frac{\pi i}{2}\right)}{\sinh \frac{\pi i}{2}} = \frac{p\left(\frac{\pi i}{2}\right)}{i},$$

the residue theorem yields

$$\begin{align} \int_{-\infty}^\infty \frac{p(x)}{\cosh x}\,dx &= 2\pi\, p\left(\frac{\pi i}{2}\right) + \int_{\pi i-\infty}^{\pi i+\infty} \frac{p(z)}{\cosh z}\,dz\\ &= 2\pi\, p\left(\frac{\pi i}{2}\right) - \int_{-\infty}^\infty \frac{p(x+\pi i)}{\cosh x}\,dx\\ &= 2\pi\, p\left(\frac{\pi i}{2}\right) - \sum_{k=0}^{\deg p} \frac{(\pi i)^k}{k!}\int_{-\infty}^\infty \frac{p^{(k)}(x)}{\cosh x}\,dx.\tag{1} \end{align}$$

Since $\cosh$ is even, only even powers of $x$ contribute to the integrals, hence we can from the beginning assume that $p$ is an even polynomial, and need only consider the derivatives of even order.

For a constant polynomial, $(1)$ yields

$$\int_{-\infty}^\infty \frac{dx}{\cosh x} = 2\pi - \int_{-\infty}^\infty \frac{dx}{\cosh x}\Rightarrow \int_{-\infty}^\infty \frac{dx}{\cosh x} = \pi.$$

For $p(z) = z^2$, we obtain

$$\begin{align} \int_{-\infty}^\infty \frac{x^2}{\cosh x}\,dx &= 2\pi \left(\frac{\pi i}{2}\right)^2 - \int_{-\infty}^\infty \frac{x^2}{\cosh x}\,dx - (\pi i)^2\int_{-\infty}^\infty \frac{dx}{\cosh x}\\ &= - \frac{\pi^3}{2} - \int_{-\infty}^\infty \frac{x^2}{\cosh x}\,dx + \pi^3, \end{align}$$

which becomes

$$\int_{-\infty}^\infty \frac{x^2}{\cosh x}\,dx = \frac{\pi^3}{4}.$$


Just for laughs, another way to use a rectangular contour involves considering the following integral

$$\oint_C dz \frac{z^2}{\sinh{z}}$$

where $C$ is the rectangle having vertices $\pm i \pi/2$ and $R \pm i \pi/2$. The contour integral is then equal to

$$i \int_0^R dx \frac{(x-i \pi/2)^2 + (x+i \pi/2)^2}{\cosh{x}} + i \int_{-\pi/2}^{\pi/2} dy \frac{(R+i y)^2}{\sinh{(R+i y)}} + \int_{-\pi/2}^{\pi/2} dy \frac{y^2}{\sin{y}}$$

Note that, in the last integral, we did not need to take the Cauchy principal value as the singularity was removed by the $y^2$ in the numerator. Thus, the last integral vanishes because the integrand is odd. The middle integral vanishes as $R \to \infty$. On the other hand, by Cauchy's theorem, the contour integral is zero for a lack of poles inside $C$. Thus,

$$i 2 \int_0^{\infty} dx \frac{x^2-\pi^2/4}{\cosh{x}} = i \int_{-\infty}^{\infty} dx \frac{x^2-\pi^2/4}{\cosh{x}}= 0$$

Using the fact that

$$\int_{-\infty}^{\infty} \frac{dx}{\cosh{x}} = \pi$$

we get

$$\int_{-\infty}^{\infty} dx \frac{x^2}{\cosh{x}} = \frac{\pi^3}{4}$$


And yet, in a third approach, we enforce the substitution $x\to \log(x)$ and write

$$\begin{align} \int_0^\infty \frac{x^2}{\cosh(x)}\,dx&=2\int_{1}^{+\infty}\frac{\log^2 x}{x^2+1}\,dx\\\\ &=2\int_0^1 \frac{\log^2 x}{x^2+1}\,dx\\\\ &= \int_{0}^{+\infty}\frac{\log^2 x}{x^2+1}\,dx \end{align}$$

Thus, the integral of interest can be expressed as

$$\bbox[5px,border:2px solid #C0A000]{\int_{-\infty}^\infty \frac{x^2}{\cosh(x)}\,dx=2 \,\int_{0}^{+\infty}\frac{\log^2 x}{x^2+1}\,dx}$$


Next, we examine the integral $I$ as given by

$$I=\oint_C \frac{\log^3(z)}{z^2+1}\,dz$$

where $C$ is the classical key-hole contour that runs along both sides of the positive real axis. Since the integrand is analytic in and on $C$, except at the simple poles, $z=\pm i$, its value is given by the residue theorem as

$$\begin{align} I&=2\pi i \text{Res}\left(\frac{\log^3(z)}{z^2+1}, z=\pm i\right)\\\\ &=2\pi i\left(\frac{\log^3(e^{i\pi/2})}{2i}+\frac{\log^3(e^{i3\pi/2})}{-2i}\right)\\\\ &=\frac{13\pi^4}{4}\,i \tag 1 \end{align}$$


Then, we note that we can write $I$ as

$$\begin{align} I&=\int_0^\infty \frac{\log^3(x)-(\log(x)+i2\pi)^3}{x^2+1}\,dx\\\\ &=-i6\pi \int_0^\infty \frac{\log^2(x)}{x^2+1}\,dx\\\\ &+12\pi^2\int_0^\infty \frac{\log(x)}{x^2+1}\,dx\\\\ &+i8\pi^3\int_0^\infty \frac{1}{x^2+1}\,dx \tag 2 \end{align}$$


The first integral on the right-hand side of $(2)$ is $1/2$ the integral of interest, the second integral is $0$ (to see this, enforce the substitution $x\to 1/x$), and the third integral is

$$\int_0^\infty \frac{1}{x^2+1}=\pi/2 \tag 3$$

Using $(1)-(3)$ reveals

$$-i6\pi \int_0^\infty \frac{\log^2(x)}{x^2+1}\,dx=\frac{13\pi^4}{4}\,i -i4\pi^4$$

and therefore, we find that

$$\bbox[5px,border:2px solid #C0A000]{\int_{-\infty}^\infty \frac{x^2}{\cosh(x)}\,dx=\frac{\pi^3}{4}}$$