简体   繁体   中英

Sympy incorrect substitution with subs

q1 = Function('q1')(t)
f=cos(q1).diff(t)
f.subs(q1,pi/2)

I have a function f = -sin(q1)*q1' and I would like to evaluate it at when q1=pi/2 . I would expect to get an answer: -(q1)' but instead I get: -0. So not only the parameter q1 gets substituted but also its time derivative becomes zero, because q1 is now a constant. Am I using a wrong method here?

In the given example, Sympy's substitution is giving the correct answer because we are trying to substitute q1(t) = pi/2 , a constant function. So it's derivative is bound to be 0 . The answer -Derivative(q1(t),t) is correct only when q1 and q1' are independent of each other, like in calculus of variations. In that case, it is better to use two different variables -- one for the q1 and another for q1' . So, if we already know the function f it is better to define it directly rather than trying to derive it like

p, q, t = symbols('p, q, t')
f = -sin(p)*q    
f.subs( p, pi/2)

This should be the correct way to substitute if q1 and q1' are independent of each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM