# Question on Reap: what does range in NDSolve affect?

Because I haven't got 50 reputation, I cannot comment directly How to find all the local minima/maxima in a range.

GetRLine3[MMStdata_, IO_: 1][x_: x] :=
ListInterpolation[#, InterpolationOrder -> IO, Method -> "Spline"][
x] & /@ (({{#[]}, #[]}) & /@ # & /@ MMStdata);
data = Transpose[{# + RandomReal[]*0.1 & /@ Range[-10, 30, 0.4],
Tanh[#] + (Sech[2 x - 0.5]/1.5 + 1.5) /. x -> # & /@
Range[-4, 4, 0.08]}];

xLimits = {Min@#1, Max@#1} & @@ Transpose[data];
f = First[100*D[GetRLine3[{data}, 3][x], x]];

vals = Reap[
soln = y[x] /.
First[NDSolve[{y'[x] == Evaluate[D[f, x]],
y[-9.9] == (f /. x -> -9.9)}, y[x], {x, -9.9, 30},
Method -> {"EventLocator", "Event" -> y'[x],
"EventAction" :> Sow[{x, y[x]}]}]]][[2, 1]];

Plot[f, {x, -9.9, 30},
Epilog -> {PointSize[Medium], Red, Point[vals]}]


My question is when I adjust the {x, -9.9, 30} in the NDSolve in Reap function, it seems only the last argument matters. As long as it is greater than or equal to 30, then nothing is affected. For example, originally

Length[vals]
(*66*)


if it is changed to {x, 12, 50}, then

Length[vals]
(*66*)


if it is changed to {x, -9.9, 15}, then

Length[vals]
(*38*)


• What version are you using? WhenEvent[] was introduced in V9 to replace the "EventLocator" method. – Michael E2 Mar 27 '19 at 22:52
• Your IC in the system is at x == -9.9, so it has to start integration at that point, no matter if you specify {x, 12, 30}. NDSolve[] only starts to save the solution (interpolating function) once x gets to 12, but the events between -9.9 and 12 still occur. – Michael E2 Mar 27 '19 at 23:44
The IC in your system is at x == -9.9, so integration has to start at that point, no matter if you specify an interval such as {x, 12, 30} that does not contain -9.9. NDSolve[] only starts to save the solution (interpolating function) once x gets to 12, but the events between -9.9 and 12 still occur and trigger the event action. (If you think about events that affect the evolution of the system, event actions must happen whether or not they occur in the interval for which the solution is being saved.)