2

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Mr. Pi
  • 391
  • 1
  • 9
  • Working code for your example: (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

1 Answers1

2

In line with suggestion from @happy fish, I would write

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;
d = {d1, d2};
ca = CoefficientArrays[Energy, d];

The list ca contains the term you are seeking, and satisfies

ca[[1]] + ca[[2]].d + d.ca[[3]].d == Energy // Simplify

Note that ca[[3]] is not symmetric, but could be replaced with (ca[[3]]+Transpose[ca[[3]])/2 if a symmetric quadratic form is required.

mikado
  • 16,741
  • 2
  • 20
  • 54