# 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] & /@ (({{#[[1]]}, #[[2]]}) & /@ # & /@ 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*)


I am confused about this, can anyone explain to me?

• What version are you using? WhenEvent[] was introduced in V9 to replace the "EventLocator" method. Mar 27, 2019 at 22:52
• I use 11.3.0.0 This should be pretty up to date. Mar 27, 2019 at 23:10
• But in the case of {x, 12, 30(or 50)}, it also gives 66. It seems the second argument does not really matter. Mar 27, 2019 at 23:32
• 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. Mar 27, 2019 at 23:44
• I originally nominated that the Q be closed, but after the OP's followup, I thought that perhaps it is not clearly explained in the documentation in the documentation that the events accumulate from the initial condition. Does anyone have a clear reference? Otherwise, I don't think that the reason is "easily found in the documentation." (I think the reason they are accumulated is easy to understand, but that's a different thing and not a reason for putting the question on hold, imho.) Mar 28, 2019 at 23:18

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.)