如何替换 sympy 表达式中的函数

发布于 2025-01-20 11:25:03 字数 1008 浏览 3 评论 0原文

假设我在 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. enter image description here). 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:

enter image description here

I am then able to subsitute quantities like 'r' with subs, e.g. vB.subs({'r':2}), which yields:

enter image description here

However, I can't figure out how to substitute the $\theta$ with a function of time. E.g. I would like to substitute enter image description hereand 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2025-01-27 11:25:03
# substitution dictionary. Note that the keys are symbols/functions
d = {r:1, theta:3.14 * t}
vB.subs(d).doit()
# -3.14*sin(3.14*t)

doit() 方法将计算导数。

# substitution dictionary. Note that the keys are symbols/functions
d = {r:1, theta:3.14 * t}
vB.subs(d).doit()
# -3.14*sin(3.14*t)

The doit() method is going to evaluate the derivative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文