2

I have a 3D rotation matrix as such:

R = sp.Matrix([
    [ sp.cos(q1 + q2),  -sp.sin(q1 + q2), 0],
    [-sp.sin(q1 + q2),   sp.cos(q1 + q2), 0],
    [               0,                 0, 1]
])

enter image description here

Where q1 and q2, are angles. One of them, q2(t) is a function of time, it changes. How can I tell that to sympy?

I would like to differentiate that matrix over time but I do not know how to do that.

Thats how I declared everything:

q1, q2, t = sp.symbols('q1 q2 t', real=True)


R = sp.Matrix([
    [ sp.cos(q1 + q2),  -sp.sin(q1 + q2), 0],
    [-sp.sin(q1 + q2),   sp.cos(q1 + q2), 0],
    [               0,                 0, 1]
])

sp.diff(R, t)

Thats the output I get from sp.diff(R, t):

enter image description here

I calculated a differential from R (by t) by hand and therefore I would like to get something like this in SymPy:

enter image description here

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
John
  • 871
  • 2
  • 9
  • 20
  • 1
    You could define `q2 = sp.Function("q2", real=True)(t)` with no other changes to your code. You could also define `q2 = sp.physics.vector.dynamicsymbols("q2", real=True)` which can support the Newton notation; see this answer for printing details: https://stackoverflow.com/questions/25346132/is-it-possible-to-implement-newtons-dot-notation-or-lagranges-prime-notation – Ehren Nov 26 '20 at 06:11

1 Answers1

1

You need to declare q1 and q2 as undefined functions, and then use them in the R matrix computed at point t.

import sympy as sp

t = sp.symbols('t', real=True)
q1 = sp.Function('q_1')
q2 = sp.Function('q_2')

R = sp.Matrix([
    [ sp.cos(q1(t) + q2(t)),  -sp.sin(q1(t) + q2(t)), 0],
    [-sp.sin(q1(t) + q2(t)),   sp.cos(q1(t) + q2(t)), 0],
    [               0,                 0, 1]
])

sp.diff(R, t)

The result that SymPy 1.7 gives is this: enter image description here

wsdookadr
  • 2,584
  • 1
  • 21
  • 44