[英]plotting orbital trajectories in python
How can I setup the three body problem in python? 如何在python中设置三体问题? How to I define the function to solve the ODEs? 如何定义解决ODE的功能?
The three equations are 这三个方程是
x'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
, x'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
,
y'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
, and y'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
,和
z'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z
. z'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z
。
Written as 6 first order we have 写成6个第一顺序我们有
x' = x2
, x' = x2
,
y' = y2
, y' = y2
,
z' = z2
, z' = z2
,
x2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
, x2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
,
y2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
, and y2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
,和
z2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z
I also want to add in the path Plot o Earth's orbit and Mars which we can assume to be circular. 我还想在路径Plot o Earth's orbit and Mars中添加我们可以认为是圆形的。 Earth is 149.6 * 10 ** 6
km from the sun and Mars 227.9 * 10 ** 6
km. 地球距离太阳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
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
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: 给定初始状态u0 = (x0, y0, z0, x20, y20, z20)
,以及时间t
的变量,可以将其输入scipy.integrate.odeint
,如下所示:
u = odeint(deriv, u0, t)
where u
will be the list as above. u
将成为上面的列表。 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
) 或者你可以解压u
从一开始,而忽略值x2
, y2
和z2
(你必须先转输出.T
)
x, y, z, _, _, _ = odeint(deriv, u0, t).T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.