我想找到一种优雅的方法来求解以下微分方程:
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))
我假设 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
dsolve(eq, phi(t))
如何更优雅地解决这个问题?
最佳答案
理想情况下,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/