I am trying to quantitatively determine the relationship between the length of two oscillating functions. Meaning, what is the duration of the green spike in relation to the blue square? Does anyone have any suggestions? These curves are the result of a NDSolve calculation for a larger model. I think that taking the derivative of the blue curve and then identifying when the derivative is both a maximum and minimum would work. However, I'm not sure how to set up WhenEvent to identify when the derivative of a curve is a maximum or minimum?

enter image description here

  • $\begingroup$ Determining the "length" of a particular spike-shaped function is somewhat arbitrary, as there are a number of possible ways in which you can define "length". One possibility is to note that since the green and blue curves have similar maximum amplitude, you could define an unnormalized length as the integral of each function over one period; computing the ratio of the resulting two numbers would then give you a rough idea of what the relative size of the green spikes is, compared to the blue squares. $\endgroup$ – DumpsterDoofus Sep 24 '14 at 1:51
  • 1
    $\begingroup$ Since NIntegrate is able to integrate numerical solutions from NDSolve, this should be fairly easy, potentially a one-liner. $\endgroup$ – DumpsterDoofus Sep 24 '14 at 1:53
  • $\begingroup$ Could you define "duration" and "length" please? $\endgroup$ – Dr. belisarius Sep 24 '14 at 2:16
  • $\begingroup$ Sure, i'm using duration and length interchangeably, which is bad on my part. But what I'm looking for is a systematic way to calculate the length (in units) along the x-axis from the time the green curve begins to rise, to the time the blue curve begins to rise. Say from approximately 160 here to 210. I can eye and guess that its 50 units, but this relationship changes in a proportional manner and I want to show this relationship. I then would compare that results to the duration that the blue curve is at a maximum, say from 90 to 160 along the x-axis. $\endgroup$ – tarhawk Sep 24 '14 at 2:25
  • $\begingroup$ One possible interpretation for "length" would be which function is the largest: measure the length of the blue as the time over which it is larger than the green and vice versa. $\endgroup$ – bill s Sep 24 '14 at 3:38

Here is a general approach, illustrated with a basic Lotka-Volterra model. In this case I simply registered events when the derivative of x is zero (crossing from above), thus there is a local maximum. This is not foolproof as there could be e.g/ a plateau like in case of your blue curve that might trigger the event multiple times due to small numerical fluctuations, but nevertheless might give you a start.

tmax = 40;
ode = {x'[t] == u x[t] - a x[t] y[t], x[0] == 1, 
       y'[t] == -v y[t] + a b x[t] y[t], y[0] == 1
      } /. {u -> 1.2, a -> 1.4, v -> 1.1, b -> 1.};
event = WhenEvent[(x'[t] < 0), {Sow@{t, x[t]}}, "DetectionMethod" -> "DerivativeSign"];
{sol, {max}} = Reap@First@NDSolve[Join[ode, {event}], {x[t], y[t]}, {t, 0, tmax}];
Plot[Evaluate[{x[t], y[t]} /. sol], {t, 0, tmax}, 
    PlotTheme -> "Scientific", Epilog -> {AbsolutePointSize@10, Green, Point@max}]

enter image description here

Calculating the pairwise differences of successive timesteps reaped whenever an event is encountered yields:


{5.50838, 5.50838, 5.50838, 5.50838, 5.50838, 5.50838}


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.