gpt4 book ai didi

python - Sympy:求解微分方程

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:12 31 4
gpt4 key购买 nike

我想找到一种优雅的方法来求解以下微分方程:

from sympy import *
init_printing()

M, phi, t, r = symbols('M phi t r')

eq = Eq(-M * phi(t).diff(t), Rational(3, 2) * m * r**2 * phi(t).diff(t) * phi(t).diff(t,t))

enter image description here

我假设 phi(t).diff(t) 不为零。因此,左侧和右侧被缩短了。

这就是我得到解决方案的方式:

# I assume d/dt(phi(t)) != 0

theta = symbols('theta')
eq = eq.subs({phi(t).diff(t, 2): theta}) # remove the second derivative
eq = eq.subs({phi(t).diff(t): 1}) # the first derivative is shortened
eq = eq.subs({theta: phi(t).diff(t, 2)}) # get the second derivative back

enter image description here

dsolve(eq, phi(t))

enter image description here

如何更优雅地解决这个问题?

最佳答案

理想情况下,dsolve() 能够直接求解方程,但它不知道如何求解(它需要了解它可以因式分解方程并独立求解因式)。我开了一个issue为了它。

我唯一的其他建议是直接将 phi 分开:

eq = Eq(eq.lhs/phi(t).diff(t), eq.rhs/phi(t).diff(t))

你也可以使用

eq.xreplace({phi(t).diff(t): 1})

在不修改二阶导数的情况下用 1 替换一阶导数(与 subs 不同,xreplace 没有关于它替换什么的数学知识;它只是准确地替换表达式) .

不要忘记 phi(t) = C1 也是一个解决方案(当 phi' 等于 0 时)。

关于python - Sympy:求解微分方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340217/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com