# Convert DE to first order?

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!

• The code isn't copyable as a valid Mathematica expression.
– Jens
Commented Aug 13, 2015 at 21:14
• Did you try following this example wolfram.com/xid/0v5g4hmcblbdp-imlwho. Are you still having problems after that? Commented Aug 13, 2015 at 21:38
– Jens
Commented Aug 13, 2015 at 21:47
• @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.
– Jens
Commented Aug 13, 2015 at 23:52

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]} =
ControlDEqnsnonaffinestatespaceForm[
eqs, {x[t], y[t], z[t], θ[t], ϕ[t]}, {}, {}, t, #[[1 ;; 2]] &,
DescriptorStateSpace -> False];


The resulting expressions (formatted for readability):

\[ScriptE]// MatrixForm


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


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

• 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. Commented Aug 13, 2015 at 22:54
• 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. Commented Aug 14, 2015 at 15:31
• 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. Commented Aug 14, 2015 at 15:57
• 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). Commented Aug 14, 2015 at 16:04
• Thanks again. Thus far, I had never bothered to hover over an image on MSE! Commented Aug 14, 2015 at 16:16

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


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