# Quadratic form derivative in Mathematica

How to correctly differentiate quadratic form by vector in Mathematica, i.e.:

$$Q=\omega^T I_{p} \omega$$

$$\frac{dQ}{d\omega}= ???$$

Clear["Derivative"];

ClearAll["Global*"];

Q = Transpose[\[CapitalOmega]].Ip.\[CapitalOmega];

D[Q, \[CapitalOmega]];


There is no concept of "transposed vector" in Mathematica.

To illustrate, let's give your variables concrete values:

Ip = {{Ixx, Ixy, Ixz}, {Ixy, Iyy, Iyz}, {Ixz, Iyz, Izz}};
ω = {ωx, ωy, ωz};


Your $$Q$$ is simply

Q = ω . Ip . ω // Expand

(*    Ixx ωx^2 + 2 Ixy ωx ωy + Iyy ωy^2 + 2 Ixz ωx ωz + 2 Iyz ωy ωz + Izz ωz^2    *)


and its gradient with respect to $$\omega$$ is $$\nabla_{\omega}Q=2I_p\cdot\omega$$:

D[Q, {ω}] == 2 Ip . ω // Expand
(*    True    *)
`
• Thank you for your answer. Is it possible to get this result without writing matrices and vectors into components? I will work with Lagrangians, which have many kinetic components, and the moments of inertia depend on generalized coordinates. It would be desirable to have the resulting equations of motion in matrix-vector form.
– ayr
Commented Jul 7, 2021 at 18:44
• Possibly. Please clarify your question with this new information. Commented Jul 7, 2021 at 19:20
• I've had much success using @CarlWoll's MatrixD package for similar tasks -- mathematica.stackexchange.com/questions/138708/… Commented Jul 7, 2021 at 23:18
• There's also this package for differentiating. More general than Carl Woll's package, but also harder to use, see example github.com/ssmit1986/DualNumbers/issues/1 Commented Jul 7, 2021 at 23:18
• @Roman Show in symbolic form what happens if $I_p$ depends on parameter $\theta$, i.e. $I_p(\theta)$ and $\omega(t)$, and find $\frac{d}{dt}(\frac{dQ}{d\omega})-\frac{dQ}{d\omega}$
– ayr
Commented Jul 12, 2021 at 6:59