VitOne
VitOne

Reputation: 469

Coefficent Matrix in Python - Group elements

I have obtatained this matrix in Python using Sympy:

> Matrix([[-THETA*l*m2*omega**2*cos(omega*t) + X*k*cos(omega*t) -
> X*omega**2*(m1 + m2)*cos(omega*t)], [THETA*g*cos(omega*t) -
> THETA*l*omega**2*cos(omega*t) - X*omega**2*cos(omega*t)]])

I now need to find an expression like:

[coefficient matrix]*(unknowns vectors)

Where my (unknowns vector) is:

Matrix([[X],[THETA]]).

I tried to use solve, simplify and collect from Sympy without success (I can only get errors or a [] return).

Upvotes: 1

Views: 76

Answers (1)

asmeurer
asmeurer

Reputation: 91580

Take the Jacobian:

In [16]: a.jacobian(Matrix([X, THETA]))
Out[16]:
⎡              2                                2              ⎤
⎢k⋅cos(ω⋅t) - ω ⋅(m₁ + m₂)⋅cos(ω⋅t)      -l⋅m₂⋅ω ⋅cos(ω⋅t)     ⎥
⎢                                                              ⎥
⎢             2                                      2         ⎥
⎣           -ω ⋅cos(ω⋅t)             g⋅cos(ω⋅t) - l⋅ω ⋅cos(ω⋅t)⎦

In [17]: a.jacobian(Matrix([X, THETA]))*Matrix([X, THETA])
Out[17]:
⎡              2              ⎛              2                   ⎞⎤
⎢- THETA⋅l⋅m₂⋅ω ⋅cos(ω⋅t) + X⋅⎝k⋅cos(ω⋅t) - ω ⋅(m₁ + m₂)⋅cos(ω⋅t)⎠⎥
⎢                                                                 ⎥
⎢             ⎛                2         ⎞      2                 ⎥
⎣       THETA⋅⎝g⋅cos(ω⋅t) - l⋅ω ⋅cos(ω⋅t)⎠ - X⋅ω ⋅cos(ω⋅t)        ⎦

In [22]: a
Out[22]:
⎡              2                              2                   ⎤
⎢- THETA⋅l⋅m₂⋅ω ⋅cos(ω⋅t) + X⋅k⋅cos(ω⋅t) - X⋅ω ⋅(m₁ + m₂)⋅cos(ω⋅t)⎥
⎢                                                                 ⎥
⎢                                 2               2               ⎥
⎣     THETA⋅g⋅cos(ω⋅t) - THETA⋅l⋅ω ⋅cos(ω⋅t) - X⋅ω ⋅cos(ω⋅t)      ⎦

By the way, if you use theta or Theta (not all uppercase), SymPy will print it as the actual greek letter theta:

In [24]: symbols('theta')
Out[24]: θ

Upvotes: 1

Related Questions