Plot3D + WhenEvent + NDSolve

Given:

$$\begin{cases} \dot{x}=-x-By^2 \\ \dot{y}=Ax-y^3 \end{cases}$$

where $$x,y$$ - variables;

$$A=[2;4],B=[0.2;2]$$ - positive parameters;

My task is to find the time $$t_n$$ of the first intersection of a variable with zero. I do this with code:

pars = {A = 2, B = 1}

{sol, points} =
Reap@NDSolve[{x'[t] == -y[t] - B x[t]^2, y'[t] == A x[t] - y[t]^3,
x[0] == y[0] == 1, WhenEvent[x'[t] == 0, Sow[{t, x'[t]}]]}, {x,
y}, {t, 15}];

Plot[{Evaluate[x'[t] /. sol]}, {t, 0, 3},
Epilog -> {PointSize[Medium], Red, Point @@ points},
PlotRange -> Full, PlotPoints -> 200]

tn = points[[1, 1]][[1]]


How to build surfaces $$t_n(A,B)$$ for the specified range of parameters A and B?

• Your code is not consistent with the equations shown, i.e., x'[t] == -y[t] - B x[t]^2 versus x'[t] == -x[t] - B y[t]^2 Commented Aug 9, 2021 at 11:48

Try

tn[A_?NumericQ, B_?NumericQ] :=
Block[{X},X = NDSolveValue[{x'[t] == -y[t] - B x[t]^2, y'[t] == A x[t] - y[t]^3, x[0] == y[0] == 1,
WhenEvent[x'[t] == 0, "StopIntegration"]}, x,{t,15}];
X["Domain"][[1, 2]] (*returns time of WhenEvent*)
]


plot tn[A,B]

Plot3D[tn[A, B], {A, 2, 4}, {B, .2, 2},AxesLabel -> {A, B, tn}]


• Yes ! This is a good option ! Tell me please, how can the $Plot$ be get from the surface now ? For example $t_n,A$ or $t_n,B$
– ayr
Commented Aug 9, 2021 at 14:10
• You mean lines A,B=constant? Try Plot[tn[A,1], {A, 2, 4}] Commented Aug 9, 2021 at 14:12
• Probably yes, I think. Commented Aug 9, 2021 at 14:32
• Seems to be a new question... I don't think that WhenEvent is able to solve this task "looking in th future". You have to check after the simulation is completed. Commented Aug 9, 2021 at 14:54
• Fine, you're welcome! Commented Aug 9, 2021 at 15:07