在python中绘制轨道轨迹

问题描述

如您所显示的,您可以将其编写为一个包含六个一阶ode的系统:

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)一个time变量t,可以这样输入scipy.integrate.odeint

u = odeint(deriv, u0, t)

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

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

解决方法

如何在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