# Simple code to write differential equations in a matrix formulation

Is there a simple code to transform the following differential system equations :

SysDiff = {Subscript[F, x] == m Subscript[x, G]''[t],
-g m + Subscript[F, y] == m Subscript[y, G]''[t],
1/2 l Sin[γ[t]] Subscript[F, x] - 1/2 l Cos[γ[t]] Subscript[F, y] == JG γ''[t]}


In a matrix formulation such as:

$$M q''(t) + \phi_q^{\mathsf{T}}*Lambda = F$$

where $q = \{γ[t], x_G[t], y_G[t]\}$ is a vector with 3 components,

and $Lambda = \{F_x, F_y\}$ is a vector with 2 components.

P.S: For a better understand, these equations are those of a simple pendulum with absolute coordinates and the constraints equations are:

SysCon = {-(1/2) l Cos[\[Gamma][t]] + Subscript[x, G][t] ==
0, -(1/2) l Sin[\[Gamma][t]] + Subscript[y, G][t] == 0}


In others words, for the equations given, how can I determine :

• the matrix M
• the matrix $\phi_q^{\mathsf{T}}$,
• the vector F ?
• What is Lambda? Subscript[[Phi], Subscript[q]] makes no sense. Is it Subscript[[Phi], q]? Then what is $\phi$. – Suba Thomas Nov 12 '15 at 16:17
• The second line of code should be written as M.D[q[t], t, t] + Subscript[ϕ, Subscript[q]].Lambda == F, and the second Subscript probably should be removed. – bbgodfrey Nov 12 '15 at 16:18

SysDiff = {
Subscript[F, x] == m Subscript[x, G]''[t],
-g m + Subscript[F, y] == m Subscript[y, G]''[t],
1/2 l Sin[γ[t]] Subscript[F, x] - 1/2 l Cos[γ[t]] Subscript[F, y] == JG γ''[t]};

eqns = SysDiff /. Equal[lhs_, rhs_] :> lhs - rhs;

q = {γ[t], Subscript[x, G][t], Subscript[y, G][t]};

Join[q2 = D[q, {t, 2}], λ ={Subscript[F, x], Subscript[F, y]}] -> 0];


The resulting matrices:

M = D[eqns, {q2}] /. c


{{0, -m, 0}, {0, 0, -m}, {-JG, 0, 0}}

Subscript[ϕ, q] = Transpose[D[eqns, {λ}] /. c]


{{1, 0, 1/2 l Sin[γ[t]]}, {0, 1, -(1/2) l Cos[γ[t]]}}

And the r.h.s vector:

(*rhs F*)f = -eqns /. c


{0, g m, 0}

• The formulation of my differential equations is right. It is the differential equations of a multibody systems with kinematic constraints. But, I have forgotten the transpose on the jacobian matrix. May you help me to correct it in my question ? I have difficulties to write a supercript in tex. – Bendesarts Nov 12 '15 at 17:44
• What are the kinematic constraints in SysDiff. It cannot be the last one as it seems to contain force terms. But now I understand what you mean by $\lambda$ - a Lagrange multiplier. – Suba Thomas Nov 12 '15 at 17:47
• OK. Sorry. I better understand you remarks. Lambda is indeed a Lagrange Multiplier. The vector Lambda corresponds in my case at the reactions force {Fx,Fy} and in my formulation with matrix the F vector is in fact {0,-m*g}. Concerning the kinematic constraints, I didn't give them here. Thank you for your help – Bendesarts Nov 12 '15 at 17:53
• I have corrected the definition of Lambda in my question – Bendesarts Nov 12 '15 at 17:55
• I think it is very confusing to be using $\lambda$ and the Jacobian of the constraints $T$ when constraints are irrelevant to your question. – Suba Thomas Nov 12 '15 at 18:00