解决系统的ODE时,Sympy返回“关键错误”

时间:2018-07-16 15:42:42

标签: python math sympy differential-equations

我正在尝试使用初始值求解测地线方程:

enter image description here

以上是两个ODE的系统。在代码中,x 1 = T(s)和x 2 = P(s)和Γ a bc < / sub> = Ga(a,b,c)

我的代码如下所示:

from gravipy import *
from sympy import *

def distance(t1,p1,t2,p2):
    init_printing()
    R = 1737
    dimensions = 2
    t,p = symbols("t p")
    x = Coordinates("x",[t,p])
    Metric = diag(R**2, R**2*sin(t)**2)
    g = MetricTensor("g", x, Metric)
    Ga = Christoffel("Ga", g)


    T,P = symbols("T P", cls=Function)
    s = symbols("s")
    ics = {T(1):t1, T(2):t2, P(1):p1, P(2):p2}
    system=[]

    coords = [T(s),P(s)]
    for a in range(dimensions):
        eq = coords[a].diff(s,s)
        for b in range(dimensions):
            for c in range(dimensions):
                christ = Ga(a+1,b+1,c+1).replace("t","T(s)")

                eq += christ * coords[b].diff(s) * coords[c].diff(s)

        system.append(Eq(eq,0))

    print(system)
    T,P = dsolve(system, [T(s), P(s)], ics=ics)

    print(T,P)

    coords=[T(s),P(s)]
    integral = 0
    for mu in range(dimensions):
        for nu in range(dimensions):
            integral += g(mu,nu).replace("t","T(s)") * coords[mu].diff(s) * coords[nu].diff(s)

    print(integrate(sqrt(integral), (s, 1, 2)))

distance(1,1,2,2)

但是,当我运行程序时,它会生成方程式系统:

[Eq(-3017169*sin(2*T(s))*Derivative(P(s), s)**2/2 + Derivative(T(s), s, s), 0), Eq(3017169*sin(2*T(s))*Derivative(P(s), s)*Derivative(T(s), s) + Derivative(P(s), s, s), 0)]

或更可读:

enter image description here

,但是在运行dsolve()时失败,并显示错误:

  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\solvers\ode.py", line 584, in dsolve
    match = classify_sysode(eq, func)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\solvers\ode.py", line 1377, in classify_sysode
    if not order[func]:
KeyError: P(s)

P(s)T(s)之间切换的位置。似乎在对ODE进行分类时会发生错误,这是否意味着sympy无法求解方程式?

1 个答案:

答案 0 :(得分:1)

请检查文档,但我相信dsolve用于使用线性代数和LU分解求解方程组。

我认为您想要一种Runge Kutta 5阶积分方法-查看ode。

您有四个耦合的一阶ODE:

dM/ds = d^2T/ds^2 = your first equation
dN/ds = d^2P/ds^2 = your second equation
dT/ds = M
dP/ds = N