Switched linear systems
Like the error message says, I believe WhenEvent
needs to change a state variable, not its (highest) derivative. Here's an approach that sets A
as a DiscreteVariable
that can be changed when needed.
listProduct[x_List] := Times @@ x;
A1 = {{0, -1}, {2, 0}};
A2 = {{0, -2}, {1, 0}};
sol = NDSolve[{x'[t] == A[t].x[t], x[0] == {6, 3}, A[0] == A2,
WhenEvent[listProduct[x[t]] <= 0, A[t] -> A1],
WhenEvent[listProduct[x[t]] > 0, A[t] -> A2]}, {x, A}, {t, 0, 100},
DiscreteVariables -> {A}][[1]];
Plot[Sign[listProduct[x[t] /. sol]], {t, 0, 100}]
listProduct
is by rm -rf from this answer.
You can use Piecewise
in the vector form of the ODE, the only tricky part is how to create the condition. Here are two possibilities:
pm1 = {{0, 1}, {1, 0}};
sol1 = NDSolveValue[
{
x'[t] == Piecewise[{{A2.x[t], x[t].pm1.x[t]>0}}, A1.x[t]],
x[0] == {6, 3}
},
x,
{t, 0, 100}
];
sol2 = NDSolveValue[
{
x'[t] == Piecewise[{{A2.x[t], Indexed[x[t], 1] Indexed[x[t], 2] > 0}}, A1.x[t]],
x[0] == {6, 3}
},
x,
{t, 0, 100}
];
Visualizations:
Plot[
{Indexed[sol1[t],1], Indexed[sol1[t], 2]},
{t,0,100},
PlotRange->All
]
Plot[
{Indexed[sol2[t],1], Indexed[sol2[t], 2]},
{t,0,100},
PlotRange->All
]