I came across the question of what percent of numbers from 0 to 1 have a logerithm whose floor is even. What I did was I made a piecewise function: Piecewise[{{1, EvenQ[Floor[Log[x]]] == True}}, 0]
. If I plot the function I get:
so it clearly recognizes this function. However, when I try to integrate the function from 0 to 1 with Integrate[Piecewise[{{1, EvenQ[Floor[Log[x]]] == True}}, 0], {x, 0, 1}]
, the result that it gives me is 0
. This is clearly false, as all outputs are either 0 or 1. I tried integrating from .1 to 1 and I still get 0. I even tried integrating from 0.2 to 0.3 where the only outputs are 1, and therefore the integral should be 0.1, but I get an output of 0. I tried doing it with conditionals, but I still get the output as 0. Is there something I am doing wrong or is Mathematica just failing to integrate it?
2 Answers
I think this gives the answer, if I didn't get mixed up about even/odd.
Sum[Integrate[Boole[Mod[n, 2] == 0], {x, Exp[-n], Exp[-n + 1]}],
{n, Infinity}]
(* 1/(1 + E) *)
I couldn't figure out how to get Integrate
(or DSolve
) to handle the condition on Log[x]
. But here's a way to approximate it with NDSolve
:
Block[{x0 = 1/10^6},
NDSolveValue[{y'[x] == sgn[x], y[1] == 0,
WhenEvent[Mod[Log[x], 1] == 0, sgn[x] -> 1 - sgn[x]],
sgn[1] == 0}, -y[x0], {x, x0, 1}, DiscreteVariables -> sgn]
]
(* 0.268941 *)
1/(1 + E) // N
(* 0.268941 *)
Addendum
To evaluate to arbitrary precision:
Block[{prec = 100},
NIntegrate[
Piecewise[{{1, 0 <= Mod[u, 2] < 1}}, 0] Exp[u],
{u, Log[10^-prec], Sequence @@ Range[Ceiling@Log[10^-prec], 0]},
WorkingPrecision -> prec]
] - 1/(1 + E)
(* -0.*10^-101 *)
-
1$\begingroup$ A simpler version is
Block[{x0 = 10^-6}, NDSolveValue[{int'[x] == Mod[Ceiling[Log[x]], 2], int[x0] == 0}, int[1], {x, x0, 1}]]
$\endgroup$ Commented Jun 16, 2022 at 18:47 -
$\begingroup$ @CarlWoll Thanks. At some point I gave up on things like
Mod@Floor@Log@x
, before I got toNDSolve
. $\endgroup$ Commented Jun 16, 2022 at 19:00
Ok I fixed it. Thank you to Coolwater for the suggestion. I did
Integrate[
Piecewise[{{1, Floor[Floor[Log[x]]/2] == Floor[Log[x]]/2}}, 0], {x, 0, 1}]
which got rid of the EvenQ
but still checked that it was even. I then used NIntegrate
to get an approximate answer and it works, giving me the expected ~0.27. I am still unsure of why it could plot the function but not integrate it, but I got it working.
-
$\begingroup$ What result do you get by executing the integral? It does not evaluate, when I run it. $\endgroup$ Commented Jun 17, 2022 at 1:44
EvenQ[a]
evaluates toFalse
immidiately for symbolic input, thats why you get 0, to avoid it use something without the Q, e.g.a/2 ∈ Integers
$\endgroup$Mod[...<Integer-valued expression>..., 2] == 0
is a standard way to "algebraically" represent the condition for being even. SoMod[Floor[Log[x]], 2] == 0
. But one problem, I think, is that there are infinitely many singular points on the interval of integration. I don't know if/when Mma is/will be programmed to handle that inIntegrate
. $\endgroup$