Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a system of differential equations which is solved correctly. However, I want to trigger two events, one of which depends on the "time elapsed" after of the first event trigger. Is it possible to ever satisfy the second WhenEvent function:

Manipulate[Plot[Evaluate[y[t] /. NDSolve[{
  y'[t] == n[t]*b - a y[t],
  y[0] == 0,
  n[0] == 1,
  WhenEvent[y[t] > y0, {n[t] -> 0, timeEvent1 = t}],
  WhenEvent[Mod[t, timeEvent1 + delay] == 0 && y[t] < y0, n[t] -> 1]},
 y, {t, 0, 100}, DiscreteVariables -> {n[t] \[Element] {0, 1}}, 
MaxSteps -> \[Infinity]]],
{t, 0, 100}],
{b, .2, 2}, {a, .1, 2}, {delay, 1, 10}, {y0, 1.5, 20}]

where n[t] is a DiscreteVariable. In my system, when n[t]->0, y[t] begins to decrease. What I would like to happen can be summarized in the below pseudo-code:

when y[t] reaches a certain level, y[t] begin to decay,
once y[t] decays after a given time elapse of "delay", if y[t] falls below the
above mentioned level, it beings to be produced again.
Rinse and repeat.

I hope this was clear enough. I'm hoping to get oscillations like this:

real desired

I think my issue is that the Mod[t,time+delay]==0 is never satisfied due to the nature of the variable t during the integration and n[t] never gets switched back to 1. Event 1 is triggered, but Event 2 is never triggered even though y[t] falls below yo and a time of time+delay has elapsed. Please let me know if I need to provide more information.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

you need to declare timeEvent as a DiscreteVariable:

 Manipulate[
    Plot[Evaluate[
      y[t] /. (ff = 
         NDSolve[{y'[t] == n[t]*b - a y[t], y[0] == 0, n[0] == 1, 
           timeEvent1[0] == 0,
             WhenEvent[y[t] > y0, { n[t] -> 0, timeEvent1[t] -> t}],
              WhenEvent[t - timeEvent1[t] == delay, n[t] -> 1 ]}, 
                   y, {t, 0, 100}, 
                    DiscreteVariables -> {n[t] \[Element] {0, 1}, timeEvent1[t]}, 
                 MaxSteps -> \[Infinity]])], {t, 0, 100}], {b, .2, 2}, 
                    {a, .1,   2}, {delay, 1, 10}, {y0, 1.5, 20}]

enter image description here

share|improve this answer
    
Great, thank you for your answer! –  tquarton May 22 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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