I'm curious whether it is possible to solve switched linear systems within the framework of NDSolve
. For example a system of linear ode's like
$$x'(t) = \left\{\begin{array}{ll} A_1 x(t),& \text{if} \,\, x_1x_2\leq 0 \\ A_2 x(t), & \text{if} \,\, x_1x_2>0 \end{array}\right.$$
where $A_1$ and $A_2$ are two constant matrices with appropriate size (namely $A_1,A_2 \in \mathbb{R}^{2\times 2}$) and $x(t) = \left(x_1(t),x_2(t)\right)^\top$.
I tried WhenEvent
but received an error message saying
"Warning: the rule !(*SuperscriptBox[\"x\", \"[Prime]\", MultilineFunction->None][t] -> A1 . x[t]) will not directly set the state because the left-hand side is not a list of state variables."
Here is the code
A1 = {{0, -1}, {2, 0}};
A2 = {{0, -2}, {1, 0}};
x[t_] = {x1[t], x2[t]}
NDSolve[{x'[t] == A2.x[t], x1[0] == 6, x2[0] == 3,
WhenEvent[x1[t] x2[t] <= 0, x'[t] -> A1.x[t]]}, {x1, x2}, {t, 0,
100}, Method -> {"EquationSimplification" -> "Residual"}]
Piecewise
? $\endgroup$ – Daniel Lichtblau Oct 8 '18 at 14:50NDSolve[]
will set up theWhenEvent[]
objects on your behalf if you usePiecewise[]
. Still, it is useful to know how to adaptWhenEvent[]
in case the automatic method fails. $\endgroup$ – J. M.'s ennui♦ Oct 8 '18 at 15:01x1
andx2
scalars and not vectors? Try usingIndexed[]
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions. $\endgroup$ – J. M.'s ennui♦ Oct 8 '18 at 15:05x[t_] = {x1[t], x2[t]}
is enough for Mathematica to know the relationship betweenx[t]
and{x1[t],x2[t]}
. $\endgroup$ – Chris K Oct 8 '18 at 15:47