1
$\begingroup$
sol = NDSolve[{x''[t] == -x[t]*t + a[t]*x[t], x[0] == 1, x'[0] == 0, 
   WhenEvent[x[t] == 0, Tb[t] -> t], 
   WhenEvent[t > Tb[t] + 0.5, a[t] -> 1], 
   WhenEvent[t <= Tb[t] + 0.5, a[t] -> 0], Tb[0] == 0, a[0] == 0}, {x,
    Tb, a}, {t, 0, 10}, DiscreteVariables -> {Tb, a}]

I'm trying to solve a differential equation with some discrete components (Tb[t],a[t]). Tb[t] is supposed to be a stair function that records the times at which x[t]=0. Tb[t] is working as intended: enter image description here

a[t] however, is not working as intended. a[t] was supposed to shift between 0 and 1 whenever (t-(Tb[t]+0.5)) is equal to zero, but as we can see per the pictures below, a[t] only does the first shift correctly, and then stagnates at 1 when it should be shifting back and forth.

enter image description here

How can I make this work correctly.

$\endgroup$

1 Answer 1

3
$\begingroup$

Something like this? I changed the changing of a[t] from working with two events to one depending on a single event.

sol = 
NDSolve[
  {
    x''[t] == -x[t]*t + a[t]*x[t], x[0] == 1, x'[0] == 0, 
     WhenEvent[x[t] == 0, Tb[t] -> t],
     WhenEvent[t - Tb[t] - 0.5 == 0, a[t] -> 1 - a[t]],
     Tb[0] == 0,
     a[0] == 0
  }, {x, Tb, a}, {t, 0, 10}, DiscreteVariables -> {Tb, a}]


Plot[sol[[1, 3, 2]][t], {t, 0, 10}]

Mathematica graphics

$\endgroup$

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.