2015-10-26 261 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時)。