Switched linear systems
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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{beginarrayll A_1 x(t),& textif ,, x_1x_2leq 0 \ A_2 x(t), & textif ,, x_1x_2>0 endarrayright.$$
where $A_1$ and $A_2$ are two constant matrices with appropriate size (namely $A_1,A_2 in mathbbR^2times 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"]
differential-equations
 |Â
show 1 more comment
up vote
1
down vote
favorite
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{beginarrayll A_1 x(t),& textif ,, x_1x_2leq 0 \ A_2 x(t), & textif ,, x_1x_2>0 endarrayright.$$
where $A_1$ and $A_2$ are two constant matrices with appropriate size (namely $A_1,A_2 in mathbbR^2times 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"]
differential-equations
1
I wonder if it might work with the rhs expressed usingPiecewise
?
â Daniel Lichtblau
3 hours ago
Indeed, if memory serves,NDSolve
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.
â J. M. is somewhat okay.â¦
2 hours ago
1
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
1
Why are the initial conditions for bothx1
andx2
scalars and not vectors? Try usingIndexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.
â J. M. is somewhat okay.â¦
2 hours ago
1
It doesn't seem that definingx[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship betweenx[t]
andx1[t],x2[t]
.
â Chris K
2 hours ago
 |Â
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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{beginarrayll A_1 x(t),& textif ,, x_1x_2leq 0 \ A_2 x(t), & textif ,, x_1x_2>0 endarrayright.$$
where $A_1$ and $A_2$ are two constant matrices with appropriate size (namely $A_1,A_2 in mathbbR^2times 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"]
differential-equations
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{beginarrayll A_1 x(t),& textif ,, x_1x_2leq 0 \ A_2 x(t), & textif ,, x_1x_2>0 endarrayright.$$
where $A_1$ and $A_2$ are two constant matrices with appropriate size (namely $A_1,A_2 in mathbbR^2times 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"]
differential-equations
differential-equations
edited 26 mins ago
Carl Woll
59.9k279154
59.9k279154
asked 3 hours ago
freddy90
34317
34317
1
I wonder if it might work with the rhs expressed usingPiecewise
?
â Daniel Lichtblau
3 hours ago
Indeed, if memory serves,NDSolve
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.
â J. M. is somewhat okay.â¦
2 hours ago
1
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
1
Why are the initial conditions for bothx1
andx2
scalars and not vectors? Try usingIndexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.
â J. M. is somewhat okay.â¦
2 hours ago
1
It doesn't seem that definingx[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship betweenx[t]
andx1[t],x2[t]
.
â Chris K
2 hours ago
 |Â
show 1 more comment
1
I wonder if it might work with the rhs expressed usingPiecewise
?
â Daniel Lichtblau
3 hours ago
Indeed, if memory serves,NDSolve
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.
â J. M. is somewhat okay.â¦
2 hours ago
1
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
1
Why are the initial conditions for bothx1
andx2
scalars and not vectors? Try usingIndexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.
â J. M. is somewhat okay.â¦
2 hours ago
1
It doesn't seem that definingx[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship betweenx[t]
andx1[t],x2[t]
.
â Chris K
2 hours ago
1
1
I wonder if it might work with the rhs expressed using
Piecewise
?â Daniel Lichtblau
3 hours ago
I wonder if it might work with the rhs expressed using
Piecewise
?â Daniel Lichtblau
3 hours ago
Indeed, if memory serves,
NDSolve
will set up the WhenEvent
objects on your behalf if you use Piecewise
. Still, it is useful to know how to adapt WhenEvent
in case the automatic method fails.â J. M. is somewhat okay.â¦
2 hours ago
Indeed, if memory serves,
NDSolve
will set up the WhenEvent
objects on your behalf if you use Piecewise
. Still, it is useful to know how to adapt WhenEvent
in case the automatic method fails.â J. M. is somewhat okay.â¦
2 hours ago
1
1
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
1
1
Why are the initial conditions for both
x1
and x2
scalars and not vectors? Try using Indexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.â J. M. is somewhat okay.â¦
2 hours ago
Why are the initial conditions for both
x1
and x2
scalars and not vectors? Try using Indexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.â J. M. is somewhat okay.â¦
2 hours ago
1
1
It doesn't seem that defining
x[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship between x[t]
and x1[t],x2[t]
.â Chris K
2 hours ago
It doesn't seem that defining
x[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship between x[t]
and x1[t],x2[t]
.â Chris K
2 hours ago
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
4
down vote
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.
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
add a comment |Â
up vote
1
down vote
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
]
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
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.
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
add a comment |Â
up vote
4
down vote
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.
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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.
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.
answered 2 hours ago
Chris K
5,76221738
5,76221738
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
add a comment |Â
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
What version of mathematica do you use? I tried your code with version 10.2 and I can only integrate till t = 4 without waiting forever
â freddy90
1 hour ago
2
2
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
v11.3 -- did you try a fresh kernel?
â Chris K
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
jep, tried that but unfortunately doesn't change anythingâ¦
â freddy90
1 hour ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
Yeah, my colleague's v10.3 has the same problem. Bug?
â Chris K
14 mins ago
add a comment |Â
up vote
1
down vote
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
]
add a comment |Â
up vote
1
down vote
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
]
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
]
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
]
answered 28 mins ago
Carl Woll
59.9k279154
59.9k279154
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183379%2fswitched-linear-systems%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
I wonder if it might work with the rhs expressed using
Piecewise
?â Daniel Lichtblau
3 hours ago
Indeed, if memory serves,
NDSolve
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.â J. M. is somewhat okay.â¦
2 hours ago
1
Tried this: system = x'[t] == Piecewise[A1.x[t], x1[t] x2[t] < 0, A2.x[t], x1[t] x2[t] > 0]; NDSolve[system, x1[t] == 3, x2[0] == 2, x1, x2, t, 0, 10]. I got the error: "Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions"
â freddy90
2 hours ago
1
Why are the initial conditions for both
x1
andx2
scalars and not vectors? Try usingIndexed
if you want to refer to a vector-valued function componentwise, just like in your inequality conditions.â J. M. is somewhat okay.â¦
2 hours ago
1
It doesn't seem that defining
x[t_] = x1[t], x2[t]
is enough for Mathematica to know the relationship betweenx[t]
andx1[t],x2[t]
.â Chris K
2 hours ago