Switched linear systems

The name of the pictureThe name of the pictureThe name of the pictureClash 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"]









share|improve this question



















  • 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 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




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




    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














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









share|improve this question



















  • 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 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




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




    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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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






  • 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 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




    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












  • 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 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




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




    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







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










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]


Mathematica graphics



listProduct is by rm -rf from this answer.






share|improve 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

















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
]


enter image description here



Plot[
Indexed[sol2[t],1], Indexed[sol2[t], 2],
t,0,100,
PlotRange->All
]


enter image description here






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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]


    Mathematica graphics



    listProduct is by rm -rf from this answer.






    share|improve 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














    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]


    Mathematica graphics



    listProduct is by rm -rf from this answer.






    share|improve 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












    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]


    Mathematica graphics



    listProduct is by rm -rf from this answer.






    share|improve 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]


    Mathematica graphics



    listProduct is by rm -rf from this answer.







    share|improve this answer












    share|improve this answer



    share|improve 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
















    • 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










    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
    ]


    enter image description here



    Plot[
    Indexed[sol2[t],1], Indexed[sol2[t], 2],
    t,0,100,
    PlotRange->All
    ]


    enter image description here






    share|improve this answer
























      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
      ]


      enter image description here



      Plot[
      Indexed[sol2[t],1], Indexed[sol2[t], 2],
      t,0,100,
      PlotRange->All
      ]


      enter image description here






      share|improve this answer






















        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
        ]


        enter image description here



        Plot[
        Indexed[sol2[t],1], Indexed[sol2[t], 2],
        t,0,100,
        PlotRange->All
        ]


        enter image description here






        share|improve 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
        ]


        enter image description here



        Plot[
        Indexed[sol2[t],1], Indexed[sol2[t], 2],
        t,0,100,
        PlotRange->All
        ]


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 28 mins ago









        Carl Woll

        59.9k279154




        59.9k279154



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery