在python中绘制轨道轨迹 [英] plotting orbital trajectories in python
问题描述
如何在python中设置三体问题?如何定义求解ODE的函数?
How can I setup the three body problem in python? How to I define the function to solve the ODEs?
这三个方程是
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
.
The three equations are
x'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
,
y'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
, and
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
公里,距离火星227.9 * 10 ** 6
公里.
I also want to add in the path Plot o Earth's orbit and Mars which we can assume to be circular.
Earth is 149.6 * 10 ** 6
km from the sun and Mars 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
推荐答案
如所示,您可以将其编写为由六个一阶ode组成的系统:
As you've shown, you can write this as a system of six first-order ode's:
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
您可以将其另存为矢量:
You can save this as a vector:
u = (x, y, z, x2, y2, z2)
并因此创建一个返回其派生函数:
and thus create a function that returns its derivative:
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
:
Given an initial state u0 = (x0, y0, z0, x20, y20, z20)
, and a variable for the times t
, this can be fed into scipy.integrate.odeint
as such:
u = odeint(deriv, u0, t)
,其中u
将是上面的列表.或者,您可以从头开始解压缩u
,并忽略x2
,y2
和z2
的值(必须先用.T
移调输出)
where u
will be the list as above. Or you can unpack u
from the start, and ignore the values for x2
, y2
, and z2
(you must transpose the output first with .T
)
x, y, z, _, _, _ = odeint(deriv, u0, t).T
这篇关于在python中绘制轨道轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!