Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Can you post the NDSolve complete code, please? – Dr. belisarius Nov 5 '13 at 12:08
{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

share|improve this answer

Your Answer


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.