0
$\begingroup$

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.

$\endgroup$

1 Answer 1

4
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Great, thank you for your answer! $\endgroup$
    – tquarton
    May 22, 2014 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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