用scipy odeint解决python中的向量常微分方程

Gre*_*ldi 3 python vector matplotlib scipy ode

我正试图解决和涉及向量的颂歌,并且不能提出可行的答案.所以我将它分成6个分量,一个用于组件的每个时间导数,一个用于速度分量的每个时间导数.第一个值似乎是合理的,然后它跳到数百万的数字,我不知道为什么.老实说,我真的不确定如何做到这一点,我现在只是试一试.我似乎无法在网上找到任何信息,如果有这类问题的例子,可以使用一些帮助或一些链接.任何信息将非常感谢如何解决这个问题.

def dr_dt(y, t):
"""Integration of the governing vector differential equation.
d2r_dt2 = -(mu/R^3)*r with d2r_dt2 and r as vecotrs.
Initial position and velocity are given.
y[0:2] = position components
y[3:] = velocity components"""

    G = 6.672*(10**-11)
    M = 5.972*(10**24)
    mu = G*M
    r = np.sqrt(y[0]**2 + y[1]**2 + y[2]**2) 

    dy0 = y[3]
    dy1 = y[4]
    dy2 = y[5]
    dy3 = -(mu / (r**3)) * y[0]
    dy4 = -(mu / (r**3)) * y[1]
    dy5 = -(mu / (r**3)) * y[2]
    return [dy0, dy3, dy1, dy4, dy2, dy5]
Run Code Online (Sandbox Code Playgroud)

解决这个问题后,我想绘制它.它应该是一个椭圆形,但说实话,我也不确定如何做到这一点.我正考虑采取位置的大小,然后随着时间绘制它.如果有更好的方法,请随时告诉我.

谢谢.

And*_*lev 5

首先,如果你y[:3]的位置y[3:]是速度,那么dr_dt函数应该按照这个顺序返回组件.其次,为了绘制轨迹,我们可以使用优秀的matplotlib mplot3d模块,或者省略z位置和速度的分量(因此我们的运动在XY平面上),以及绘图yx.示例代码(具有更正的返回值顺序)如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def dr_dt(y, t):
    """Integration of the governing vector differential equation.
    d2r_dt2 = -(mu/R^3)*r with d2r_dt2 and r as vecotrs.
    Initial position and velocity are given.
    y[0:2] = position components
    y[3:] = velocity components"""

    G = 6.672*(10**-11)
    M = 5.972*(10**24)
    mu = G*M
    r = np.sqrt(y[0]**2 + y[1]**2 + y[2]**2).

    dy0 = y[3]
    dy1 = y[4]
    dy2 = y[5]
    dy3 = -(mu / (r**3)) * y[0]
    dy4 = -(mu / (r**3)) * y[1]
    dy5 = -(mu / (r**3)) * y[2]
    return [dy0, dy1, dy2, dy3, dy4, dy5]

t = np.arange(0, 100000, 0.1)
y0 = [7.e6, 0., 0., 0., 1.e3, 0.]
y = odeint(dr_dt, y0, t)
plt.plot(y[:,0], y[:,1])
plt.show()
Run Code Online (Sandbox Code Playgroud)

这会产生一个很好的椭圆:

在此输入图像描述