4
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ What version are you using? WhenEvent[] was introduced in V9 to replace the "EventLocator" method. $\endgroup$ – Michael E2 Mar 27 '19 at 22:52
  • $\begingroup$ I use 11.3.0.0 This should be pretty up to date. $\endgroup$ – consideration Mar 27 '19 at 23:10
  • $\begingroup$ But in the case of {x, 12, 30(or 50)}, it also gives 66. It seems the second argument does not really matter. $\endgroup$ – consideration Mar 27 '19 at 23:32
  • 4
    $\begingroup$ 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. $\endgroup$ – Michael E2 Mar 27 '19 at 23:44
  • $\begingroup$ 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.) $\endgroup$ – Michael E2 Mar 28 '19 at 23:18
3
$\begingroup$

The answer I believe is that there are 66 local extrema between x == -9.9 and x == 50, all of which are between x == -9.9 and x == 30 but of which there are only 38 extrema between x == -9.9 and x == 15.

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

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.