How to tell SymPy that a variable is a function of time?

Viewed 163

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

1 Answers

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

Related