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}]$


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


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 ?

enter image description here

  • 6
    $\begingroup$ Please post your Lagrangian as copyable code and make it as easy as possible for others to help you. $\endgroup$ Commented Jul 8, 2021 at 9:48
  • $\begingroup$ @SjoerdSmit see my edit $\endgroup$
    – ayr
    Commented Jul 9, 2021 at 12:45
  • 1
    $\begingroup$ dtn, what is the result that you are expecting to get? $\endgroup$ Commented Jul 9, 2021 at 13:02
  • $\begingroup$ @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. $\endgroup$
    – ayr
    Commented Jul 9, 2021 at 13:05
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jul 9, 2021 at 13:27

1 Answer 1


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
Grad[L, omega]
  • $\begingroup$ Thank you for your answer! I was hoping to get a similar result using Mathematica (without attracting extraneous packages ): ibb.co/8YhNWF2 $\endgroup$
    – ayr
    Commented Jul 9, 2021 at 18:09
  • $\begingroup$ 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]; $\endgroup$
    – ayr
    Commented Jul 9, 2021 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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