6
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – Bob Hanlon
    Aug 9, 2021 at 11:48

1 Answer 1

8
$\begingroup$

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}]

enter image description here

$\endgroup$
9
  • $\begingroup$ 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$ $\endgroup$
    – dtn
    Aug 9, 2021 at 14:10
  • 1
    $\begingroup$ You mean lines A,B=constant? Try Plot[tn[A,1], {A, 2, 4}] $\endgroup$ Aug 9, 2021 at 14:12
  • 1
    $\begingroup$ Probably yes, I think. $\endgroup$ Aug 9, 2021 at 14:32
  • 1
    $\begingroup$ 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. $\endgroup$ Aug 9, 2021 at 14:54
  • 1
    $\begingroup$ Fine, you're welcome! $\endgroup$ Aug 9, 2021 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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