2015-10-26 263 views
2

我想找到解决以下微分方程的一种优雅的方式:Sympy:求解微分方程

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

我假设岛(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

如何解决这个更优雅?

回答

2

理想情况下dsolve()将能够直接求解方程,但它不知道如何(它需要知道它是如何计算方程并独立求解的)。我为它打开了一个issue

我的唯一的其他建议是划分披”出来直接:

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

也可以使用

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

具有1至替换一阶导数,而无需修改所述第二导数(不像subsxreplace没有关于它正在替代的数学知识;它只是完全替换表达式)。

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