Bug introduced in 9.0 or earlier and fixed in 10.1
In exploring What is the range of values $x$ for which $f(x)$ is higher than $k$ over a given domain?, I came across the following strange behavior:
With[{a = 0, b = 2, k = 0.1, f = Sin[10 #] &},
Last@Reap[
NDSolve[{ifun[t] == f[t],
in[a] == Boole[f[a] > k], x1[a] == a,
s'[t] == 1, s[a] == a,
WhenEvent[f[t] > k, {in[t] -> 1, x1[t] -> t}],
WhenEvent[f[t] < k, If[in[t] == 1, Sow[{x1[t], t}]]; {in[t] -> 0}]},
{}, {t, a, b}, DiscreteVariables -> {in, x1}]]
]
(* {{{0.0100167, 0.304143}, {0.638335, 0.932461}, 6.76508*10^-8, {1.26665, 1.56078}}} *)
Where does the 6.76508*10^-8
come from? Only pairs are sown. The output should be the same as the following:
Partition[x /. NSolve[Sin[10 x] == 1/10 && 0 < x < 2, x], 2]
(* {{0.0100167, 0.304143}, {0.638335, 0.932461}, {1.26665, 1.56078}} *)
(V10.0.2, Mac OSX 10.10.1)
{{{0.0100167, 0.304143}, 1.27866*10^-8, 1.27866*10^-8, 4.12524*10^-8, {0.638335, 0.932461}, {1.26665, 1.56078}, 9.32835*10^-8}}
$\endgroup$Sow[expr, tag]
the extra reaps aren't shown,so there are probably spuriousSow
s in theNDSolve[]
code $\endgroup$If[in[t] == 1, {x1[t], t}]
(without theSow
) and leave in theReap
you also reap something. As @belisarius mentions this is a spurious Sow with out a tag in NDSolve. Kernel code can not use Sow without a tag for exactly this reason. I'll file a bug and someone has to dig through it.... $\endgroup$