# Calculate state space matrices from the Lagrangian

I have a Lagrangian with two inputs (Tin1,Tin2) and two variables (x1[t], x2[t]). I want to calculate the linear state space model for it using Matlab or Mathematica. The state variables should be x1[t], x2[t], x1'[t], x2'[t]. Is there a way for it?

L = 0.5*m1*(l1/2)^2*(x1'[t])^2 + 0.5*I1*(x1'[t])^2 - 0.5*m1*g*l1*Sin[x1[t]] +
0.5*I2*(x1'[t])^2 +       0.5*m2*l1^2*(x1'[t])^2 + 0.5*m2*(l2/2)^2*(x1'[t] +
x2'[t])^2 + 0.5*m2*l1*l2*x1'[t]*(x1'[t] + x2'[t]) Cos[x2[t]] +
0.5*I2*(x2'[t])^2 - m2*g*l1*Sin[x1[t]] - 0.5*m2*g*l2*Sin[x1[t] + x2[t]] + I2*x1'[t]*x2'[t];


I know the below codes should be the first step of the whole calculation:

D[D[L, x1'[t]], t] - D[L, x1[t]] == Tin1
D[D[L, x2'[t]], t] - D[L, x2[t]] == Tin2


There is an example in the documentation.

eqns = Table[D[D[L, Derivative[1][q][t]], t] - D[L, q[t]] ==
Subscript[T, q][t], {q, {x1, x2}}];

StateSpaceModel[eqns, {x1[t], x2[t]}, {Subscript[T, x1][t],
Subscript[T, x2][t]}, {x1[t], x2[t]}, t]


• I could not find this example in the documentation unfortunately @Suba Thomas
– F R
Dec 12, 2017 at 15:52
• The link I have in the answer reference.wolfram.com/language/ref/… shows how to get an AffineStateSpaceModel. It is the same workflow for a StateSpaceModel. What else do you need. Dec 12, 2017 at 16:01
• well, I used the AffineStateSpaceModel but did not work! I have it like this: sys1 = D[D[L, x1'[t]], t] - D[L, x1[t]] == Tin1 sys2 = D[D[L, x2'[t]], t] - D[L, x2[t]] == Tin2 AffineStateSpaceModel[Join[sys1, sys2], {x1[t], x2[t], x1'[t], x2'[t]}, {Tin1, Tin2}, t] // Simplify @Suba Thomas
– F R
Dec 12, 2017 at 16:03
• Sure, your code is so full of errors and naturally it will not work. 1.Tin1 is not the same as Tin1[t]. 2. Is the result of your Join a list of equations? 3. What are the outputs of your AffineStateSpaceModel? Dec 12, 2017 at 16:10
• Tin1 is not defined to be time variant, so it should not be Tin1[t] (but even if it would be defined as Tin1[t] it does not work), and yes the result of the join is a list of equations. the output is x1[t] I suppose. AffineStateSpaceModel[ Join[sys1, sys2], {x1[t], x2[t], x1'[t], x2'[t]}, {Tin1, Tin2}, {x1[t]}, t] @Suba Thomas
– F R
Dec 12, 2017 at 16:23

Your statespace variables are x1[t],x2[t],x1'[t],x2'[t]

With

sys={D[D[L, x1'[t]], t] - D[L, x1[t]] == Tin1,D[D[L, x2'[t]], t] - D[L, x2[t]]==  Tin2}


and the substitution

subst={x1'[t]->v1[t],x1''[t]->v1'[t],x2'[t]->v2[t],x2''[t]->v2'[t]}
sys4=Join[sys/.subst,{x1'[t]==v1[t],x2'[t]==v2[t]}]


you get 4 ode's, which can be solved

rS = {x1'[t], v1'[t], x2'[t], v2'[t]}  /.Solve[sys4, {x1'[t], v1'[t],x2'[t], v2'[t]} ][[1]]
(*

{1. v1[t], -((-1. (I2 + 0.25 l2^2 m2 +
0.5 l1 l2 m2 Cos[x2[t]]) (-1. Tin2 +
0.5 g l2 m2 Cos[x1[t] + x2[t]] -
0.5 l1 l2 m2 Sin[x2[t]] v1[t] v2[t] +
0.5 l1 l2 m2 Sin[x2[t]] v1[t] (v1[t] + v2[t])) + (1. I2 +
0.25 l2^2 m2) (-1. Tin1 + 0.5 g l1 m1 Cos[x1[t]] +
g l1 m2 Cos[x1[t]] + 0.5 g l2 m2 Cos[x1[t] + x2[t]] -
0.5 l1 l2 m2 Sin[x2[t]] v1[t] v2[t] -
0.5 l1 l2 m2 Sin[x2[t]] v2[t] (v1[t] + v2[t])))/(0. +
1. I1 I2 + 0.25 I2 l1^2 m1 + 1. I2 l1^2 m2 + 0.25 I1 l2^2 m2 +
0.0625 l1^2 l2^2 m1 m2 + 0.25 l1^2 l2^2 m2^2 -
0.25 l1^2 l2^2 m2^2 Cos[x2[t]]^2)),
1. v2[t], -((0.5 (2.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2^2 -
0.25 l1^2 l2^2 m2^2 Cos[x2[t]]^2)))}
*)


The nonlinear statespace differentialequation is of the form {x1'[t], v1'[t], x2'[t], v2'[t]}==rS . Linearization of rS gives the linear statespace model.

• Hi, thanks, that gives just a differential equations diffinition for each state, and not the linearized state space model in a form of x'=Ax+Bu @Ulrich Neumann
– F R
Dec 12, 2017 at 14:45
• Sorry, my answer is completed. Dec 12, 2017 at 15:51
• well, I could not get an answer form this code, there is an error in it, did you get the result from this? @Ulrich Neumann
– F R
Dec 12, 2017 at 15:55
• @F R: The four lines of code in my answer are ok, again tested in my notebook Dec 12, 2017 at 16:01
• Can you post the state space that you get here? @Ulrich Neumann
– F R
Dec 12, 2017 at 16:05