绘制在python一个球体轨道轨迹 [英] plotting a sphere in python for an orbital trajectory

查看:1394
本文介绍了绘制在python一个球体轨道轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以把半径 1737 一个球体的位置(384400,0,0)

该领域将是我的轨迹月球。

其他的一切与code是很好,我只是不知道如何添加一个球体与该半径位置。

 进口numpy的为NP
从scipy.integrate进口odeint
进口matplotlib.pyplot为PLT
从mpl_toolkits.mplot3d进口Axes3D

我= 5.974 * 10 **(24)#质量的地球
毫米= 7.348 * 10 ** 22#质量月亮
G = 6.67259 * 10 **(-20)#引力参数
重新= 6378.0#半径公里的地球
RM = 1737.0#半径公里的月球
R12 = 384400.0#地球和月亮的COM之间的距离
M =我+毫米

PI1 =我/ M
PI2 =毫米/ M
地球公里^ 3 /秒^ 2 = MUE#398600.0重力参数
妈妈= G * MM#GRAV月亮的参数
亩= MUE +妈妈
欧米茄= np.sqrt(亩/ R12 ** 3)
NU = -129.21 * np.pi / 180#真近的弧度角

X = 327156.0  -  4671
#x的位置,月球的SOI效果随着偏移飞船
#地球不是在(0,0),在地月系统
Y = 33050.0#y位置

VBO = 10.85#速度在职业倦怠

伽马= 0 * np.pi / 180#在飞行路径的弧度角

VX = VBO *(np.sin(伽马)* np.cos(NU) -  np.cos(伽马)* np.sin(NU))
在X方向#速度博
VY = VBO *(np.sin(伽马)* np.sin(NU)+ np.cos(伽马)* np.cos(NU))
在y方向#速度博

xrel =(RE + 300.0)* np.cos(NU) -  PI2 * R12
#宇宙飞船x位置相对于地球
yrel =(RE + 300.0)* np.sin(NU)

#r0的= [xrel,yrel,0]
#V0 = [VX,VY,0]
U0 = [xrel,yrel,0,VX,VY,0]


高清DERIV(U,DT):
    N1 =  - ((MUE *(U [0] + PI2 * R12)/ np.sqrt((U [0] + PI2 * R12)** 2
                                               + U [1] ** 2)** 3)
         - (妈妈*(U [0]  -  PI1 * R12)/ np.sqrt((U [0]  -  PI1 * R12)** 2
                                              + U [1] ** 2)** 3))
    N2 =  - ((MUE * U [1] / np.sqrt((U [0] + PI2 * R12)** 2 + U [1] ** 2)** 3)
         - (妈妈* U [1] / np.sqrt((U [0]  -  PI1 * R12)** 2 + U [1] ** 2)** 3))
    返回[U [3],#dotu [0] = U [3]
            U [4],#dotu [1] = U [4]
            U [5],#dotu [2] = U [5]
            2 *ω-* U [5] +欧米加** 2 * U [0] + N1,#dotu [3] =那
            欧米加** 2 * U [1]  -  2 *ω-* U [4] + N2,#dotu [4] =那
            0]#dotu [5] = 0


DT = np.arange(0.0,320000.0,1)#200000秒运行模拟
U = odeint(DERIV,U0,DT)
的x,y和z,X2,Y2,Z2 = u.T

图= plt.figure()
AX = fig.add_subplot(111,投影='3D')
ax.plot(X,Y​​,Z)
plt.show()
 

解决方案

您可以添加以下code绘制球体,在 plt.show()

 披= np.linspace(0,2 * np.pi,100)
THETA = np.linspace(0,np.pi,100)
XM = RM * np.outer(np.cos(PHI),np.sin(THETA))+ R12
YM = RM * np.outer(np.sin(PHI),np.sin(THETA))
ZM = RM * np.outer(np.ones(np.size(PHI)),np.cos(THETA))
ax.plot_surface(XM,YM,ZM)
 

不过,你的月亮看起来都伸了出来,因为规模不等于对所有轴。为了改变轴的刻度,你可以添加一些像

  ax.auto_scale_xyz([ -  50000,400000],[0,160000],[-130000,130000])
 

plt.show()。其结果是还没有完全正确的,但我把它留给你玩,以获得更好的结果 - 我只是挑一些数字,使得它看起来有点好

How can I put a sphere of radius 1737 at the location of (384400,0,0)?

This sphere would be the moon in my trajectory.

Everything else with the code is fine, I just don't know how to add a sphere in that location with that radius.

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

me = 5.974 * 10 ** (24)  #  mass of the earth                                     
mm = 7.348 * 10 ** (22)  #  mass of the moon                                      
G = 6.67259 * 10 ** (-20)  #  gravitational parameter                             
re = 6378.0  #  radius of the earth in km                                         
rm = 1737.0  #  radius of the moon in km                                          
r12 = 384400.0  #  distance between the CoM of the earth and moon                 
M = me + mm

pi1 = me / M
pi2 = mm / M
mue = 398600.0  #  gravitational parameter of earth km^3/sec^2                    
mum = G * mm  #  grav param of the moon                                           
mu = mue + mum
omega = np.sqrt(mu / r12 ** 3)
nu = -129.21 * np.pi / 180  #  true anomaly angle in radian                       

x = 327156.0 - 4671
#  x location where the moon's SOI effects the spacecraft with the offset of the  
#  Earth not being at (0,0) in the Earth-Moon system                              
y = 33050.0   #  y location                                                       

vbo = 10.85  #  velocity at burnout                                               

gamma = 0 * np.pi / 180  #  angle in radians of the flight path                   

vx = vbo * (np.sin(gamma) * np.cos(nu) - np.cos(gamma) * np.sin(nu))
#  velocity of the bo in the x direction                                          
vy = vbo * (np.sin(gamma) * np.sin(nu) + np.cos(gamma) * np.cos(nu))
#  velocity of the bo in the y direction                                          

xrel = (re + 300.0) * np.cos(nu) - pi2 * r12
#  spacecraft x location relative to the earth         
yrel = (re + 300.0) * np.sin(nu)

#  r0 = [xrel, yrel, 0]                                                           
#  v0 = [vx, vy, 0]                                                               
u0 = [xrel, yrel, 0, vx, vy, 0]


def deriv(u, dt):
    n1 = -((mue * (u[0] + pi2 * r12) / np.sqrt((u[0] + pi2 * r12) ** 2
                                               + u[1] ** 2) ** 3)
        - (mum * (u[0] - pi1 * r12) / np.sqrt((u[0] - pi1 * r12) ** 2
                                              + u[1] ** 2) ** 3))
    n2 = -((mue * u[1] / np.sqrt((u[0] + pi2 * r12) ** 2 + u[1] ** 2) ** 3)
        - (mum * u[1] / np.sqrt((u[0] - pi1 * r12) ** 2 + u[1] ** 2) ** 3))
    return [u[3],  #  dotu[0] = u[3]                                              
            u[4],  #  dotu[1] = u[4]                                              
            u[5],  #  dotu[2] = u[5]                                              
            2 * omega * u[5] + omega ** 2 * u[0] + n1,  #  dotu[3] = that         
            omega ** 2 * u[1] - 2 * omega * u[4] + n2,  #  dotu[4] = that         
            0]  #  dotu[5] = 0                                                    


dt = np.arange(0.0, 320000.0, 1)  #  200000 secs to run the simulation            
u = odeint(deriv, u0, dt)
x, y, z, x2, y2, z2 = u.T

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()

解决方案

You can add the following code to draw the sphere, before the plt.show():

phi = np.linspace(0, 2 * np.pi, 100)
theta = np.linspace(0, np.pi, 100)
xm = rm * np.outer(np.cos(phi), np.sin(theta)) + r12
ym = rm * np.outer(np.sin(phi), np.sin(theta))
zm = rm * np.outer(np.ones(np.size(phi)), np.cos(theta))
ax.plot_surface(xm, ym, zm)

However, your moon will look all stretched out because the scale is not equal for all axes. In order to change the scales of the axes, you can add something like

ax.auto_scale_xyz([-50000, 400000], [0, 160000], [-130000, 130000])

before plt.show(). The result is still not completely right, but I leave it up to you to play around to get better results -- I just picked some numbers that make it look somewhat better.

这篇关于绘制在python一个球体轨道轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆