在python中绘制轨道轨迹

时间:2013-04-17 00:19:30

标签: python numpy matplotlib scipy differential-equations

如何在python中设置三体问题?如何定义解决ODE的功能?

这三个方程是
x'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
y'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
z'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z

写成6个第一顺序我们有

x' = x2

y' = y2

z' = z2

x2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x

y2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y

z2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z

我还想添加地球轨道和火星的路径,我们可以假设它是圆形的。 地球离太阳149.6 * 10 ** 6 km 227.9 * 10 ** 6 km。

#!/usr/bin/env python                                                             
#  This program solves the 3 Body Problem numerically and plots the trajectories      

import pylab
import numpy as np
import scipy.integrate as integrate
import matplotlib.pyplot as plt
from numpy import linspace

mu = 132712000000  #gravitational parameter
r0 = [-149.6 * 10 ** 6, 0.0, 0.0]
v0 = [29.0, -5.0, 0.0]
dt = np.linspace(0.0, 86400 * 700, 5000)  # time is seconds

1 个答案:

答案 0 :(得分:8)

如你所示,你可以把它写成六个一阶颂歌的系统:

x' = x2
y' = y2
z' = z2
x2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
y2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
z2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z

您可以将其另存为矢量:

u = (x, y, z, x2, y2, z2)

因此创建一个返回其衍生物的函数:

def deriv(u, t):
    n = -mu / np.sqrt(u[0]**2 + u[1]**2 + u[2]**2)
    return [u[3],      # u[0]' = u[3]
            u[4],      # u[1]' = u[4]
            u[5],      # u[2]' = u[5]
            u[0] * n,  # u[3]' = u[0] * n
            u[1] * n,  # u[4]' = u[1] * n
            u[2] * n]  # u[5]' = u[2] * n

给定初始状态u0 = (x0, y0, z0, x20, y20, z20)和时间t的变量,可以将其输入scipy.integrate.odeint

u = odeint(deriv, u0, t)

其中u将是上面的列表。或者您可以从一开始就解压缩u,并忽略x2y2z2的值(您必须首先使用.T转置输出)

x, y, z, _, _, _ = odeint(deriv, u0, t).T