I am using NDSolve to solve a set of coupled differential equations. When a certain event occurs, I want two equations in the set to be changed radically, for example from

x'[t] == a*y[t] + b*y'[t], y[t] == c*x[t] + d*x'[t]


x'[t] == 0, y[t] == -f*y'[t]

For this purpose, I have first tried to use the trick shown in the Mathematica featured example for the ACDC Full Wave Rectifier. So I just wrote the equations like

x'[t] == go[t]*(a*y[t] + b*y'[t]), y[t] == go[t]*(c*x[t] + d*x'[t]) + (1-go[t])*(-f*y'[t])

and set events like

WhenEvent[{y[t] > y0 && go[t]==1}, go[t] -> 0]

WhenEvent[{y[t] < y0 && go[t]==0}, go[t] -> 1]

to jump back and forth between the two pairs of equations I want to use each time the event is triggered. However, two 'division by zero' errors occur (I assume it's because NDSolve is still trying to solve the parts multiplied by zero). So apparently this method does not work for this type of problem.

Anybody knows the 'right' way to do this?

  • 2
    $\begingroup$ Can you post the NDSolve complete code, please? $\endgroup$ Commented Nov 5, 2013 at 12:08

1 Answer 1

{a, b, c, d, f} = {1, 1, 1, 1, -1/10};
yy = .006;
s[{x0_, y0_}] :=
    x'[t] == go[t] (a y[t] + b y'[t]), 
    y[t] == go[t]*(c x[t] + d x'[t]) + (1 - go[t]) (-f y'[t]),
    go[0] == 1,
    y[0] == y0,
    x[0] == x0,
    WhenEvent[Abs[y[t] - yy] < .0001, go[t] -> Abs[go[t] - 1]]},
   {x, y}, {t, 0, 8},
   DiscreteVariables -> go];

Plot[Through[s[{1, 1}][t]], {t, 0, 2}, Evaluated -> True]

Mathematica graphics


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.