6

Mathematica fails with

Integrate[(-1)^Floor[1/x]/x, {x, 0, 1}]

How can I evaluate it?

In view of it, I unsuccessfully try

NIntegrate[(-1)^Floor[1/x]/x, {x, 0, 1}, AccuracyGoal -> 4, 
Method -> {"GlobalAdaptive", "MaxErrorIncreases" -> 10000, 
Method -> "GaussKronrodRule"}, MaxRecursion -> 20]

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 20 recursive bisections in x near {x} = {6.08983877753243723225686862226814152545986400616302122725995044034*10^-325}. NIntegrate obtained 710.5859812076798and 3.956615692341655 for the integral and error estimates.

710.586

This question is inspired by this discussion.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user64494
  • 26,149
  • 4
  • 27
  • 56

5 Answers5

10

Mathematica needs to be helped a bit for this problem, as neither Integrate[] nor NIntegrate[] are sufficiently smart enough.

We proceed as in a hand calculation: letting $n\in\mathbb N$, we consider the integral

$$\int_{\frac1{n+1}}^{\frac1n}\frac{(-1)^{\lfloor\frac1x\rfloor}}{x}\mathrm dx$$ and then sum up all the terms.

Mathematica is still unable to deal with this integral, but evaluating it for increasing $n$ reveals a pattern:

Table[Integrate[(-1)^Floor[1/x]/x, {x, 1/(n + 1), 1/n}], {n, 8}]
   {-Log[2], Log[3/2], -Log[4/3], Log[5/4], -Log[6/5], Log[7/6], -Log[8/7], Log[9/8]}

and we find that the general term is $(-1)^n\log\left(1+\frac1n\right)$. (I tried to use FindSequenceFunction[] on this sequence, but it took too long and I lost patience.)

A plausibility check:

Block[{$MaxPiecewiseCases = 120, n = 100}, 
      Integrate[(-1)^Floor[1/x]/x, {x, 1/(n + 1), 1}] == 
      Sum[(-1)^k Log[1 + 1/k], {k, n}] // Simplify]
   True

and the OP's integral is thus equivalent to evaluating

$$\sum_{n=1}^\infty (-1)^n\log\left(1+\frac1n\right)$$

which Sum[] is unable to do, but is easily dealt with by NSum[]:

NSum[(-1)^k Log[1 + 1/k], {k, 1, ∞}, Method -> "AlternatingSigns", WorkingPrecision -> 40]
   -0.451582705289454864726195229894882608267

If you really must use NIntegrate[], one could generate the sequence of integrals

$$\int_{\frac1n}^1\frac{(-1)^{\lfloor\frac1x\rfloor}}{x}\mathrm dx$$

and accelerate the convergence rate of the sequence using the Wynn $\varepsilon$ algorithm, via NumericalMath`NSequenceLimit[] (SequenceLimit[] in earlier versions):

NumericalMath`NSequenceLimit[Table[
              NIntegrate[(-1)^Floor[1/x]/x, {x, 1/n, 1},
                         Method -> {"SymbolicPiecewiseSubdivision",
                                    "MaxPiecewiseCases" -> (n + 2)},
                         WorkingPrecision -> 50], {n, 2, 30}]]
   -0.45158270528945486472619524

Note the use of "SymbolicPiecewiseSubdivision" to ensure the splitting of the integral into intervals where a polynomial approximation will be fine, and "MaxPiecewiseCases" will ensure that the whole business is split up.


Mariusz's answer shows how to get a closed form answer. Here's one way to do it in Mathematica.

First, note the following:

{D[(-1)^(n - 1)/(n + 1)^z, z], D[(-1)^n/n^z, z]} /. z -> 0
   {(-1)^n Log[1 + n], (-1)^(1 + n) Log[n]}

Thus,

Sum[D[(-1)^(n - 1)/(n + 1)^z, z], {n, 1, ∞}] +
Sum[D[(-1)^n/n^z, z], {n, 1, ∞}] /. z -> 0 // FullSimplify
   Log[2/π]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
10

The OP's integral is thus equivalent to evaluating the sum:

$$\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)$$

Using identity: $$\int_0^{\infty } \frac{(1-\exp (-t)) \exp (-n t)}{t} \, dt=\ln\left(1+\frac{1}{n}\right)$$ puting to sum:

$$\sum _{n=1}^{\infty } \frac{(-1)^n ((1-\exp (-t)) \exp (-n t))}{t}=-\frac{1-e^{-t}}{t+e^t t}$$ and integrating:

$$\int_0^{\infty } -\frac{1-e^{-t}}{t+e^t t} \, dt=\ln \left(\frac{2}{\pi }\right)$$ Closed form solution: $$\color{blue}{\int_0^1 \frac{(-1)^{\left\lfloor \frac{1}{x}\right\rfloor }}{x} \, dx=\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)=\ln \left(\frac{2}{\pi }\right)}$$

EDITED:

Solution by: Maple 2018

enter image description here


Alternative how find closed form solution ?: $$\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)=\sum _{n=1}^{\infty } -(-1)^n \ln (n)+\sum _{n=1}^{\infty } (-1)^n \ln (n+1)$$ By Regularization Sums

 Sum[-(-1)^n*Log[n], {n, 1, Infinity}, Regularization -> "Abel"] + 
 Sum[(-1)^n*Log[n + 1], {n, 1, Infinity}, Regularization -> "Abel"] // FullSimplify

 (* Log[2/π] *)

A Feynman's trick:

s1 = Sum[-(-1)^n*n^k, {n, 1, Infinity}] + 
Sum[(-1)^n*(n + 1)^k, {n, 1, Infinity}] // FullSimplify

Limit[D[s1, k], k -> 0] // FullSimplify

(* Log[2/π] *)
Mariusz Iwaniuk
  • 13,841
  • 1
  • 25
  • 41
4

This is the same calculation from another point of view. We can realise that after the variable change $t=1/x$, the integrand is equal to:

$$\frac{(-1)^{\lfloor t\rfloor }}{t}$$

and integrated in $(1,\infty)$. After plotting the function:

enter image description here

we can calculate easily the integrals and, summing them:

NSum[Evaluate@Integrate[(-1)^Floor[n]*1/t, {t, n, n + 1}], {n, 1, ∞},
     Method -> "AlternatingSigns"]

(* -0.451583 *)

or integrating piecewisely in $(n,n+1)$, with $n\in \mathrm{N}$:

Assuming[n > 1 && n ∈ Integers, 
Integrate[(-1)^Floor[n]*t^-1, {t, n, n + 1}]]

$$(-1)^{ n } \log \left(\frac{1}{n}+1\right)$$

and summing:

NSum[(-1)^n Log[1 + 1/n], {n, 1, ∞}, Method -> "AlternatingSigns"]

(* -0.451583 *)

By using the Frullani's integral identity proposed by @MariuszIwaniuk:

$$\int_{(0,\infty)} \frac{f(ax)-f(bx)}{x} dx=(f(0)-f(\infty)) \log \frac{b}{a}$$

his closed form is obtained:

$$\int_0^1 \frac{(-1)^{\left\lfloor \frac{1}{x}\right\rfloor }}{x} \, dx=\sum _{n=1}^{\infty } (-1)^n \log \left(1+\frac{1}{n}\right)=\log \left(\frac{2}{\pi }\right)$$

where $\log$ is the natural logarithm in this case.

4
 Sum[(-1)^n Log[1 + 1/n], {n, 1, Infinity}]

returns unevaluated, but the following product gives a closed form

 Log[Product[(1 + 1/(2*k)) / (1 + 1/(2*k - 1)), {k, 1, Infinity}]]

 (* Log[2/Pi] *)

 N[%, 40]

 (* -0.4515827052894548647261952298948821435718 *)
Vaclav Kotesovec
  • 2,949
  • 1
  • 11
  • 22
2

Slightly more automized

Block[{x, n},
  NSum[
     (-1)^n Evaluate[ Integrate[1/x, {x, 1/(n + 1), 1/n}, Assumptions -> n > 0]], 
     {n, 1, ∞}]
  ] // AbsoluteTiming

{0.806038, -0.451583}

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309