I deal with structural problems where I have to obtain the stiffness or mass matrix from the kinetic and potential energy of the system. For example:
Energy =
1/2 (d1^2/2 + (-(d1/Sqrt[2]) + d2/Sqrt[2])^2) k - d1 P - d2 P -
(d1^2/(2 L^2) + d2^2/(2 L^2)) P
Now I take the derivative of the expression with respect to d1 then with respect to d2, then I have to collect the terms like this:
{{k - P/L^2, -(k/2)}, {-(k/2), k/2 - P/L^2}}.{d1, d2}
Of course there's the vector {P, P}, but my point of interest is if I can obtain the matrix above without having to collect the terms and create the matrix myself especially when I have to deal with problems with higher degrees of freedom.
(CoefficientList[#, {d1, d2}] & /@ ((D[ 1/2 (d1^2/2 + (-(d1/Sqrt[2]) + d2/Sqrt[2])^2) k - d1 P - d2 P - (d1^2/(2 L^2) + d2^2/(2 L^2)) P, #] // Simplify) & /@ {d1, d2})) /. {{_, y_}, {x_, 0}} :> {x, y}– vapor Jul 03 '16 at 15:37