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?
WhenEvent[]was introduced in V9 to replace the"EventLocator"method. – Michael E2 Mar 27 '19 at 22:52x == -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) oncexgets to12, but the events between-9.9and12still occur. – Michael E2 Mar 27 '19 at 23:44