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.

Anyone knows what's the problem with the following code and how can I fix it? I am not sure that I wrote the WhenEvent section correctly. By the way, I want to solve the equations and then plot y vs. y' when x goes to zero (it is obvious that it's an interval :) ).


tmax = 300;
{{sol1}, {ps}} = Reap[
 {x'[t] == -(x[t] + 2 x[t] y[t]),
  y'[t] == -x[t] x[t] + y[t] (y[t] - 1),
  x[0] == 0.1, y[0] == 0.1,
  WhenEvent[Limit[x[t], x[t] -> 0], Sow[t]]},
 {x, y},    
 {t, 0, tmax}]]
share|improve this question

1 Answer 1

The condition in WhenEvent should be Boolean (True/False). The function Limit yields a number when the limit exists. In your case, the limit is 0, provided x is an undefined symbol.*

Perhaps you want something like this:

tmax = 300;
{{sol1}, {ps}} = 
 Reap[NDSolve[{x'[t] == -(x[t] + 2 x[t] y[t]), 
    y'[t] == -x[t] x[t] + y[t] (y[t] - 1), x[0] == 0.1, y[0] == 0.1, 
    WhenEvent[x[t] == 0, Sow[t]]}, {x, y}, {t, 0, tmax}]]

(* Out:
 {{{x -> InterpolatingFunction[{{0., 300.}}, "<>"],
    y -> InterpolatingFunction[{{0., 300.}}, "<>"]}},
  {{20.5968}}} *)

*In general Limit[f[t], f[t] -> 0] won't evaluate if f is defined, say, f[t_] := 1+t. (Mathematically Limit represents the limit of the first expression, treating the second as an independent variable.)

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.