6
$\begingroup$

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.

$\endgroup$

5 Answers 5

10
$\begingroup$

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/π]
$\endgroup$
3
  • $\begingroup$ +1. Nice, but rather by hand with help of Mathematica. The question remains open: how to NIntegrate that? $\endgroup$
    – user64494
    Mar 24, 2018 at 10:48
  • $\begingroup$ FindSequenceFunction fails with it in short time. $\endgroup$
    – user64494
    Mar 24, 2018 at 10:58
  • $\begingroup$ Now I accept it. $\endgroup$
    – user64494
    Mar 24, 2018 at 11:18
10
$\begingroup$

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/π] *)
$\endgroup$
5
  • $\begingroup$ Neat idea to use Frullani's integral. (IOU one upvote.) $\endgroup$ Mar 24, 2018 at 15:09
  • $\begingroup$ @J.M.Analytic solution is always the best solution no need to struggling with NSum or NIntegrate. $\endgroup$ Mar 24, 2018 at 15:15
  • $\begingroup$ @MariuszIwaniuk what is the role of $k$ in the sum of your closed form solution? $\endgroup$ Mar 24, 2018 at 17:26
  • $\begingroup$ @JoséAntonioDíazNavas. A typo.Corrected.Thanks.:) $\endgroup$ Mar 24, 2018 at 17:32
  • $\begingroup$ @MariuszIwaniuk, impressed how you obtained the closed-form (+1) $\endgroup$ Mar 24, 2018 at 17:37
4
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ When you changed variables, did you remember to differentiate your substitution? $\endgroup$ Mar 24, 2018 at 13:21
  • $\begingroup$ @J.M., yes, I found my error. Good catch ! thnx... $\endgroup$ Mar 24, 2018 at 16:44
4
$\begingroup$
 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 *)
$\endgroup$
3
  • $\begingroup$ How come this Product,Can you explain ? $\endgroup$ Mar 25, 2018 at 21:07
  • $\begingroup$ @Mariusz Iwaniuk: If n is even then (-1)^nLog[1 + 1/n] = Log[1 + 1/n]. If n is odd then (-1)^nLog[1 + 1/n] = -Log[1 + 1/n] = Log[1/(1 + 1/n)]. And Log[a] + Log[b] + Log[c] + ... = Log[abc*...]. It's clear now ? $\endgroup$ Mar 25, 2018 at 21:47
  • $\begingroup$ Yes it's clear. Thanks :) $\endgroup$ Mar 25, 2018 at 22:34
2
$\begingroup$

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}

$\endgroup$
5
  • $\begingroup$ No output during several minutes in version 11.3 on Windows 10. Did you check your suggestion? $\endgroup$
    – user64494
    Mar 24, 2018 at 11:07
  • 1
    $\begingroup$ @user, it works quickly in 11.2; what version are you using? $\endgroup$ Mar 24, 2018 at 11:08
  • $\begingroup$ Works, but does not result. $\endgroup$
    – user64494
    Mar 24, 2018 at 11:10
  • $\begingroup$ I use version 11.3 on macos and it returns -0.4515827052894538` after a second or so. Have you tried to clean the kernel? If x or n have values assigned to them then Evaluate might do shabby things. $\endgroup$ Mar 24, 2018 at 11:13
  • $\begingroup$ @Henrik Schumacher: Thank you for your advice. Now it works well. $\endgroup$
    – user64494
    Mar 24, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.