在python中绘制轨道轨迹

dus*_*tin 4 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

我还想在路径Plot o Earth's orbit and Mars中添加我们可以认为是圆形的.地球149.6 * 10 ** 6距离太阳和火星227.9 * 10 ** 6公里数公里.

#!/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
Run Code Online (Sandbox Code Playgroud)

ask*_*han 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
Run Code Online (Sandbox Code Playgroud)

您可以将其另存为矢量:

u = (x, y, z, x2, y2, z2)
Run Code Online (Sandbox Code Playgroud)

从而创建一个返回其衍生物的函数:

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
Run Code Online (Sandbox Code Playgroud)

给定一个初始状态u0 = (x0, y0, z0, x20, y20, z20)和一个时间变量t,可以这样输入scipy.integrate.odeint:

u = odeint(deriv, u0, t)
Run Code Online (Sandbox Code Playgroud)

u上面的列表将在哪里.或者你也可以解包u从一开始,并忽略这些值x2,y2z2(你必须先转输出.T)

x, y, z, _, _, _ = odeint(deriv, u0, t).T
Run Code Online (Sandbox Code Playgroud)