I would like to use the WhenEvent
function (link here) to set a bound on a dynamic variable in a set of ODEs. That is, when the variables k21[t]
and k12[t]
reach above specific value a
, I would like to set them back at that value. A simplified version of the problem looks like
w1 = 6/24.5;
w2 = 6/23.5;
a = 0.1;
Eqs = {
x1'[t] == w1 + (k21[t]/2)*Sin[x2[t] - x1[t]],
x2'[t] == w2 + (k12[t]/2)*Sin[x1[t] - x2[t]],
k21'[t] == a*(Cos[x2[t] - x1[t] + \[Pi]] + 1),
k12'[t] == a*(Cos[x1[t] - x2[t] + \[Pi]] + 1)};
ICs = {x1[0] == 3/2, x2[0] == 3/4, k21[0] == 0.0001, k12[0] == 0.0001};
events = {WhenEvent[Abs[k21[t]] > a, k21[t] -> a], WhenEvent[Abs[k12[t]] > a, k12[t] -> a]};
EqsICs = Join[Eqs, ICs, events];
SolutionValue[t_] = NDSolveValue[EqsICs, {x1[t], x2[t], k21[t], k12[t]}, {t, 0, 10^6}];
And I plot the solutions as:
Show[
Plot[SolutionValue[t][[3]], {t, 0, tmax}, PlotRange -> {{0, tmax}, {-0.1, 0.1}}, AxesOrigin -> {0, 0}],
Plot[SolutionValue[t][[4]], {t, 0, tmax}, PlotRange -> {{0, tmax}, {-0.1, 0.1}}, AxesOrigin -> {0, 0}]
]
However, my WhenEvent
doesn't work. The plot shows that the values of kij
keep growing beyond a
. Is my syntax wrong? Thanks :)