3
$\begingroup$

I have the following set of second-order differential equations:

eqs = {Derivative[2][x][t] + 
     l0 Sin[ϕ[
        t]] (Sin[θ[t]] (Derivative[1][θ][t]^2 + 
           Derivative[1][ϕ][t]^2) - 
        Cos[θ[t]] Derivative[2][θ][t]) - 
     l0 Cos[ϕ[
        t]] (2 Cos[θ[t]] Derivative[1][θ][
          t] Derivative[1][ϕ][t] + 
        Sin[θ[t]] Derivative[2][ϕ][t]) == 0, 
   Derivative[2][y][t] + 
     l0 Cos[θ[
        t]] (-2 Sin[ϕ[t]] Derivative[1][θ][
          t] Derivative[1][ϕ][t] + 
        Cos[ϕ[t]] Derivative[2][θ][t]) - 
     l0 Sin[θ[
        t]] (Cos[ϕ[t]] (Derivative[1][θ][t]^2 + 
           Derivative[1][ϕ][t]^2) + 
        Sin[ϕ[t]] Derivative[2][ϕ][t]) == 0, 
   l0 Cos[θ[t]] Derivative[1][θ][t]^2 + 
     Derivative[2][z][t] + 
     l0 Sin[θ[t]] Derivative[2][θ][t] == 0, 
   l0 (-g m Cos[θ[t]] - 
       l0 Cos[θ[t]] Sin[θ[t]] Derivative[1][ϕ][
          t]^2 + Cos[θ[
          t]] (-Sin[ϕ[t]] Derivative[2][x][t] + 
          Cos[ϕ[t]] Derivative[2][y][t]) + 
       Sin[θ[t]] Derivative[2][z][t] + 
       l0 Derivative[2][θ][t]) == 0, 
   l0 Sin[θ[
       t]] (2 l0 Cos[θ[t]] Derivative[1][θ][
         t] Derivative[1][ϕ][t] - 
       Cos[ϕ[t]] Derivative[2][x][t] - 
       Sin[ϕ[t]] Derivative[2][y][t] + 
       l0 Sin[θ[t]] Derivative[2][ϕ][t]) == 0};

How do I convert them into state-space form, i.e. first order differential equations? Documentation on state-space didn't help. Thanks!

$\endgroup$
  • 2
    $\begingroup$ The code isn't copyable as a valid Mathematica expression. $\endgroup$ – Jens Aug 13 '15 at 21:14
  • 1
    $\begingroup$ Did you try following this example wolfram.com/xid/0v5g4hmcblbdp-imlwho. Are you still having problems after that? $\endgroup$ – Suba Thomas Aug 13 '15 at 21:38
  • 1
    $\begingroup$ In addition to @SubaThomas advice, please also specify in what way the documentation didn't help. $\endgroup$ – Jens Aug 13 '15 at 21:47
  • 1
    $\begingroup$ @SubaThomas I think I fixed the copying problems in the question, so it should be possible to use the actual equations in our answers now. $\endgroup$ – Jens Aug 13 '15 at 23:52
6
$\begingroup$

To convert ODEs (or difference equations) to state-space form you can use the functions StateSpaceModel, AffineStateSpaceModel, or NonlinearStateSpaceModel.

The input signatures of all three functions are the same. The first argument is the set of equations, the second argument is the set of states, the third argument is the set of inputs, the fourth argument is the outputs (which are a combination of the state and input variables) and the last argument is the temporal variable. The second and third argument also allows you to specify non-zero operating points (the default is zero). The set of states and inputs are mutually exclusive and all variables that depend on the temporal variable must belong to one of these two sets.

As of now, only StateSpaceModel supports DAEs. Before converting to the state-space representation, StateSpaceModel completely linearizes the system, AffineStateSpaceModel linearizes the input variables and the highest order state derivatives, and NonlinearStateSpaceModel linearizes the highest order state derivatives.

Basic examples of these can be found on the reference pages for StateSpaceModel(link), AffineStateSpaceModel (link), and NonlinearStateSpaceModel(link).

(Update after Jens fixed the equations. Thanks Jens for fixing them.)

If we try to solve these equations for the highest order derivatives we see that there is no solution.

Solve[eqs, {x''[t], y''[t], z''[t], θ''[t], ϕ''[t]}]

{}

Thus it cannot be put into the form $\dot{\mathit{x}}=\mathit{f}( \mathit{x})$.

The equations are still linear in the highest order derivatives, and so it can be put in the form $\mathit{e}.\dot{\mathit{x}}=\mathit{f}( \mathit{x})$. You can use an internal function to do this calculation for you.

{{\[ScriptF], \[ScriptH], \[ScriptE]}, \[ScriptX]} =                        
Control`DEqns`nonaffinestatespaceForm[
eqs, {x[t], y[t], z[t], θ[t], ϕ[t]}, {}, {}, t, #[[1 ;; 2]] &,                                                
DescriptorStateSpace -> False];

The resulting expressions (formatted for readability):

\[ScriptE]// MatrixForm

enter image description here

{D[\[ScriptX], t], Table["\[DoubleLeftRightArrow]", 
10], \[ScriptF]}\[Transpose] // TableForm

enter image description here

The complication here is that $\mathit{e}$ is not invertible.

$\endgroup$
  • $\begingroup$ NonlinearStateSpaceModel only does a Taylor linearization of the highest order derivatives. I looked at the equations and it doesn't seem to have any nonlinear second derivative terms? I agree that it will be better to have good code before spending too much time on analysis. $\endgroup$ – Suba Thomas Aug 13 '15 at 22:54
  • 1
    $\begingroup$ I wonder if the output would look better as images of formatted expressions (or possibly as single lines of code, to be copied by anyone interested). It's pretty much impossible to read this way, anyway. $\endgroup$ – Michael E2 Aug 14 '15 at 15:31
  • $\begingroup$ Ok Michael E2, I think it is better now thanks to your suggestion. I think you still may have to strain a little to see the expressions. $\endgroup$ – Suba Thomas Aug 14 '15 at 15:57
  • $\begingroup$ I can see much better what is going on, if I can't make out all the subscripts. :) I can click on the image, if want to check something (or run the code). $\endgroup$ – Michael E2 Aug 14 '15 at 16:04
  • $\begingroup$ Thanks again. Thus far, I had never bothered to hover over an image on MSE! $\endgroup$ – Suba Thomas Aug 14 '15 at 16:16
4
$\begingroup$

Here is how to do it for a smaller working example:

eqs = {x''[t] == -y[t], y''[t] == x[t]};

{eqs2, {velocities}} = 
 Reap[eqs /. 
   Derivative[n_][f_][t] :> 
    Derivative[n - 1][Sow[Subscript[v, f], derivs]][t], derivs]

(*
==> {{Derivative[1][Subscript[v, x]][t] == -y[t], 
  Derivative[1][Subscript[v, y]][t] == x[t]}, {{Subscript[v, x], 
   Subscript[v, y]}}}
*)

eqs3 = Map[#[t] == D[Last[#][t], t] &, velocities]

(*
==> {Subscript[v, x][t] == Derivative[1][x][t], 
 Subscript[v, y][t] == Derivative[1][y][t]}
*)

solution = {x[t], y[t]} /. 
   First@NDSolve[
     Join[eqs2, 
      eqs3, {x[0] == 0, y[0] == 1, Subscript[v, x][0] == 1, 
       Subscript[v, y][0] == -1}], 
     Join[{x, y}, velocities], {t, 0, 10}];

Edit: application to the original equations

Here is how the above can be applied to the original equations in the question (after I fixed the copying errors):

eqs = {Derivative[2][x][t] + 
     l0 Sin[ϕ[
        t]] (Sin[θ[t]] (Derivative[1][θ][t]^2 + 
           Derivative[1][ϕ][t]^2) - 
        Cos[θ[t]] Derivative[2][θ][t]) - 
     l0 Cos[ϕ[
        t]] (2 Cos[θ[t]] Derivative[1][θ][
          t] Derivative[1][ϕ][t] + 
        Sin[θ[t]] Derivative[2][ϕ][t]) == 0, 
   Derivative[2][y][t] + 
     l0 Cos[θ[
        t]] (-2 Sin[ϕ[t]] Derivative[1][θ][
          t] Derivative[1][ϕ][t] + 
        Cos[ϕ[t]] Derivative[2][θ][t]) - 
     l0 Sin[θ[
        t]] (Cos[ϕ[t]] (Derivative[1][θ][t]^2 + 
           Derivative[1][ϕ][t]^2) + 
        Sin[ϕ[t]] Derivative[2][ϕ][t]) == 0, 
   l0 Cos[θ[t]] Derivative[1][θ][t]^2 + 
     Derivative[2][z][t] + 
     l0 Sin[θ[t]] Derivative[2][θ][t] == 0, 
   l0 (-g m Cos[θ[t]] - 
       l0 Cos[θ[t]] Sin[θ[t]] Derivative[1][ϕ][
          t]^2 + Cos[θ[
          t]] (-Sin[ϕ[t]] Derivative[2][x][t] + 
          Cos[ϕ[t]] Derivative[2][y][t]) + 
       Sin[θ[t]] Derivative[2][z][t] + 
       l0 Derivative[2][θ][t]) == 0, 
   l0 Sin[θ[
       t]] (2 l0 Cos[θ[t]] Derivative[1][θ][
         t] Derivative[1][ϕ][t] - 
       Cos[ϕ[t]] Derivative[2][x][t] - 
       Sin[ϕ[t]] Derivative[2][y][t] + 
       l0 Sin[θ[t]] Derivative[2][ϕ][t]) == 0};

{eqs2, {velocities}} = 
  Reap[eqs /. 
    Derivative[n_][f_][t] :> 
     Derivative[n - 1][Sow[Subscript[v, f], derivs]][t], derivs];

vs = DeleteDuplicates[velocities];

eqs3 = Map[#[t] == D[Last[#][t], t] &, vs];

TraditionalForm@TableForm[Join[eqs2, eqs3]]

eqs

This is the system of first-order equations which corresponds exactly to the second-order equations.

It's not clear from the question whether any further linearization is desired.

$\endgroup$
  • $\begingroup$ You could do NonlinearStateSpaceModel[eqs, {x[t], y[t]}, {}, {x[t], y[t]}, t] or even just StateSpaceModel[eqs, {x[t], y[t]}, {}, {x[t], y[t]}, t]. $\endgroup$ – Suba Thomas Aug 13 '15 at 21:43
  • $\begingroup$ @SubaThomas Would you consider expanding your comment into an answer with a few more details, either on Jens' example or on the OP's, if the OP fixes his syntax? $\endgroup$ – MarcoB Aug 13 '15 at 21:47
  • $\begingroup$ @SubaThomas That's probably what the question is after - but I don't know for sure if there's something wrong with applying NonlinearStateSpaceModel to the OP's equations, so if that's the case my pedestrian approach could be a work-around. $\endgroup$ – Jens Aug 13 '15 at 21:49
  • $\begingroup$ @Marco, I will do this shortly. @ Jens, I will elaborate this in my answer, and the the OP can see if he can use the built-in function or if he will need to improvise. $\endgroup$ – Suba Thomas Aug 13 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.