如何替换 sympy 表达式中的函数
假设我在 sympy 中有一个表达式,我可以用变量来区分(例如 )。这是一个位置表达式,我有兴趣将其对时间进行微分以获得速度。使用以下代码:
r = sp.symbols('r')
t = sp.symbols('t')
theta= sp.Function('theta')(t)
params = {'r':1,'l':2, 'theta':2*t}
dtheta = theta.diff(t)
xB = r*sp.cos(theta)
vB = f.diff(t)
我能够获得其导数的通用形式:
然后我就可以用 subs 替换像 'r' 这样的数量,例如vB.subs({'r':2})
,生成:
< img src="https://i.sstatic.net/9xV0J.png" alt="在此处输入图像描述">
但是,我不知道如何用函数替换 $\theta$的时间。例如我想替换 并获取该特定值的解决方案。
我很高兴我可以在一开始就改变这些值,但我希望这应该是可能的。
Assuming I have an expression in sympy that I can differentiate with variable (e.g. ). This is a position expression and I am interested in differentiating it with respect to time to obtain the velocity. Using the following code:
r = sp.symbols('r')
t = sp.symbols('t')
theta= sp.Function('theta')(t)
params = {'r':1,'l':2, 'theta':2*t}
dtheta = theta.diff(t)
xB = r*sp.cos(theta)
vB = f.diff(t)
I am able to obtain the generic form of the derivative for this:
I am then able to subsitute quantities like 'r' with subs, e.g. vB.subs({'r':2})
, which yields:
However, I can't figure out how to substitute the $\theta$ with a function of time. E.g. I would like to substitute and obtain the solution to that specific value.
I appreciate I can change the values right at the beginning, but I expect that this should be possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
doit()
方法将计算导数。The
doit()
method is going to evaluate the derivative.