# Problem with Lagrangian and Matrix Calculus in Mathematica

Continuing the questions:

Lagrangian of three-mass system with Mathematica

Equations of motion for two-mass torsional oscillator with the gear train

Derivation of equations of motion for a multi-body system using Mathematica

A Lagrangian is given in which the kinetic energy is written as quadratic form:

$$L=\frac{1}{2}\dot{\theta_1}^2+\frac{1}{2}\dot{\theta_2}^2+\frac{1}{2}\dot{\theta_3}^2+\frac{1}{2}\boldsymbol{\omega}^T I_2(\theta_1,\theta_2,\theta_3)\boldsymbol{\omega}+\frac{1}{2}\boldsymbol{\Omega}^T I_p(\theta_1,\theta_2,\theta_3)\boldsymbol{\Omega}+\frac{c_1(\theta_1-\boldsymbol{\omega})^2}{2}$$

where $$\omega,\Omega$$ - vectors;

$$I_2,I_p$$ - matrix;

Generalized coordinates $$\boldsymbol{q}=[\theta_1,\theta_2,\theta_3,\boldsymbol{\omega},\boldsymbol{\Omega}]$$

$$\frac{d}{dt}(\frac{dL}{d\dot{\boldsymbol{q}}})-\frac{dL}{d\boldsymbol{q}}=0$$

How to get equations of motion from $$L$$ without writing out vectors and matrices component-wise, but assuming that we are dealing only with matrices and vectors?

(those. to actually work with vector and matrix differentiation in Mathematica).

I considered the issue in the previous topic, but I could not solve it correctly.

Quadratic form derivative in Mathematica

EDIT:

There is my Lagrangian in Mathematica:

L = 1/2 Subscript[\[Theta], 1]^2 + 1/2 Subscript[\[Theta], 2]^2 +
1/2 Subscript[\[Theta], 3]^2 +
1/2 Transpose[\[Omega]] I2[Subscript[\[Theta], 1]^2,
Subscript[\[Theta], 2]^2, Subscript[\[Theta], 3]^2] \[Omega] +
1/2 Transpose[\[CapitalOmega]] Ip[Subscript[\[Theta], 1]^2,
Subscript[\[Theta], 2]^2,
Subscript[\[Theta], 3]^2] \[CapitalOmega]


When I try to get $$\frac{dL}{d\omega}$$, i get this incorrect result. Mathematica can't work with Matrix Calculus ?

• Please post your Lagrangian as copyable code and make it as easy as possible for others to help you. Commented Jul 8, 2021 at 9:48
• @SjoerdSmit see my edit
– ayr
Commented Jul 9, 2021 at 12:45
• dtn, what is the result that you are expecting to get? Commented Jul 9, 2021 at 13:02
• @CATrevillian I want to get the equation of motion through this Lagrangian in symbolic form. But kinetic energy, represented as a quadratic form, doesn't let me do it correctly. As far as I understand, Mathematica does not work with Matrix Calculus.
– ayr
Commented Jul 9, 2021 at 13:05
• dtn, it looks like some clarification was asked of you by those who have made effort to answer your question there, however, you have not yet provided that by way of editing your question with the clarifying information. That should be a good place to start by editing your previous question. I will say that I expect that you should need to tell Mathematica what exactly your variables are in the way of specifying them as matrices or otherwise. I’m not sure you need to indicate dimensions, but certainly to tell whether they are matrices or scalars or operators, etc. Commented Jul 9, 2021 at 13:27

All the objects involved should be defined before used like

theta = {ctheta[1], ctheta[2], ctheta[3]};
omega = {comega[1], comega[2], comega[3]};
Omega = {cOmega[1], cOmega[2], cOmega[3]};
I2 = Table[i2[ctheta[1], ctheta[2], ctheta[3]][i, j], {i, 1, 3}, {j, 1, 3}];
Ip = Table[ip[ctheta[1], ctheta[2], ctheta[3]][i, j], {i, 1, 3}, {j, 1, 3}];

L = 1/2 theta.theta + 1/2 omega.I2.omega + 1/2 Omega.Ip.Omega

• did I understand correctly that I should do something like this? and then look for the equation of motion through the components of the generalized coordinate (in the case of a vector)? \[Omega] = {{\[Omega]x}, {\[Omega]y}, {\[Omega]z}}; Ip = {{Ixx, Ixy, Ixz}, {Ixy, Iyy, Iyz}, {Ixz, Iyz, Izz}}; 1/2 Transpose[\[Omega]].Ip. \[Omega];