Replacing terms / sympy.physics.mechanics

25 views
Skip to first unread message

Peter Stahlecker

unread,
Mar 6, 2022, 9:18:01 AM3/6/22
to sympy
I am playing around with an ellipse, which rotates in 3D.
In its own coordinate system A, its equation of course is 

x**2/a**2 + y**2/b**2 + z**2/c**2 = 1        

A rotates relative to N, the generalized coordinates are q1, q2, q3,  dq1/dt = u1, etc.
I express the rotated ellipse in N, by using A.variable_map(N), now the coordinates are, say, N_x, N_y, N_z.
Using sympy.solve I solve the ellipse equation for N_y,  I get two solutions, of which I take the one I need, so, say, I get
N_y = f(N_x, N_z, q1, d2, d3, a, b, c,…)
I want the speed of N_y, so I write:

Speed_N_y = f(…..).diff(t)

Now my problem starts:
In Speed_N_y,  there are terms like Derivative(q1(t), t), and same for q2, q3.
Of course, these terms are u1, u2, u3, but I cannot find a way to replace Derivative(q1(t), t) with u1, etc.
I tried Speed_N_y.subs({Derivative(q1(t), t): u2})  but this did not work. 
I tried Speed_N_y.subs({q1.diff(t): u1}) also this did not work.
I also tried to get rid of these Derivative terms by writing
NY_lam = lambdify( …, Speed_N_y, {‚Derivative(q1(t), t)‘: u1})
This did not give an error - but also did not accomplish anything.

Of course, when I try to lambdify my Speed_N_y and then try to evaluate it numerically the problems come up, as this Derivative(q1(t), t) is unknown.

Any help is GREATLY appreciated! Thanks!




Jason Moore

unread,
Mar 7, 2022, 2:13:10 AM3/7/22
to sympy
I typically use `.xreplace()` if I'm simply swapping one variable for another. But subs or replace should work. Sympy mechanics uses t = me.dynamicsymbols._t internally. Are you using your own defined t that different? If soe the derivative terms could print the same but aren't actually the same symbols.

You'll need to share some code that reproduces the substitution error so we can run it and try it.

Jason

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/243c5c3b-0896-4814-8198-953b001ff950n%40googlegroups.com.

Peter Stahlecker

unread,
Mar 7, 2022, 7:23:15 AM3/7/22
to sy...@googlegroups.com
I tried on a 'small' example, and N_y.suns({sm.Derivative(q1, t): u1}) worked just fine !!
No idea, what is going on.

Jason Moore

unread,
Mar 7, 2022, 7:39:46 AM3/7/22
to sympy
The sympy.srepr() function can help debug things, as it shows the "true" form of the expression. Maybe a variable is printing differently than what it  is.

Jason

Peter Stahlecker

unread,
Mar 7, 2022, 8:19:13 AM3/7/22
to sy...@googlegroups.com
I found my mistake, matter closed.
Reply all
Reply to author
Forward
0 new messages