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

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

closed as unclear what you're asking by Michael E2, m_goldberg, Yves Klett, Dr. belisarius, Sjoerd C. de Vries Jun 14 '15 at 7:52

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Since you have not accepted my answer or otherwise responded to it, I'm assuming I had the wrong idea. In which case, the question seems unclear. Could you help us understand it better? – Michael E2 Jun 13 '15 at 15:28


I'm not convinced that in this system x[t] has a zero, if that is what you're after. When I increasing WorkingPrecision, the zero keeps getting pushed back. When the WorkingPrecision is pushed above around 200, NDSolve cannot complete the integration (at least in a reasonable time). Sometimes there is convergence failure and sometimes the limit MaxSteps -> 10^6 stops it.

tmax = 300;
{{sol1}, zeros} = 
   Reap[NDSolve[{x'[t] == -(x[t] + 2 x[t] y[t]), 
      y'[t] == -x[t] x[t] + y[t] (y[t] - 1), x[0] == 1/10, y[0] == 1/10, 
      WhenEvent[x[t] == 0, Sow[t]; "RestartIntegration"]},
     {x, y}, {t, 0, tmax}, PrecisionGoal -> 50, WorkingPrecision -> 200, 
     MaxSteps -> 10^6]]; // AbsoluteTiming
y["Domain"] /. sol1 // N
zeros // N
  {141.204, Null}
  {{0., 300.}}

Original answer

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

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