首页 > 编程问答 >绘制行星位置随时间的函数

绘制行星位置随时间的函数

时间:2024-07-27 12:55:28浏览次数:7  
标签:python ellipse orbital-mechanics

我一直在尝试模拟绕太阳运动的行星和小行星,我发现了这个链接: 如何在已经绘制的椭圆上绘制行星轨道作为时间的函数?

并且我决定研究并尝试其中的代码。但似乎我要么使用错误,要么代码错误,因为当我绘制火星、地球、木星、水星和金星的轨道时,它们似乎与美国宇航局的在线模拟不一致,例如这个: https://eyes.nasa.gov/apps/solar-system/#/home

根据美国航空航天局模拟的当前位置: 美国航空航天局模拟:

根据我的绘图的当前位置: 我的模拟

代码:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Circle

#Set axes aspect to equal as orbits are almost circular; hence square is needed
ax = plt.figure(0).add_subplot(111, aspect='equal')

#Setting the title, axis labels, axis values and introducing a grid underlay
#Variable used so title can indicate user inputed date
plt.title('Inner Planetary Orbits at[user input date]')
plt.ylabel('x10^6 km')
plt.xlabel('x10^6 km')
ax.set_xlim(-300, 300)
ax.set_ylim(-300, 300)
plt.grid()

#Creating the point to represent the sun at the origin (not to scale), 
ax.scatter(0,0,s=200,color='y')
plt.annotate('Sun', xy=(0,-30))

#Implementing ellipse equations to generate the values needed to plot an ellipse
#Using only the planet's min (m) and max (M) distances from the sun
#Equations return '2a' (the ellipses width) and '2b' (the ellipses height)
def OrbitLength(M, m):
    a=(M+m)/2
    c=a-m
    e=c/a
    b=a*(1-e**2)**0.5
    print(a)
    print(b)
    return 2*a, 2*b

#This function uses the returned 2a and 2b for the ellipse function's variables
#Also generating the orbit offset (putting the sun at a focal point) using M and m
def PlanetOrbit(Name, M, m):
    w, h = OrbitLength(M, m)
    Xoffset= ((M+m)/2)-m
    Name = Ellipse(xy=((Xoffset),0), width=w, height=h, angle=0, linewidth=1, fill=False)
    ax.add_artist(Name)

    
    
from math import *

EPSILON = 1e-12
def solve_bisection(fn, xmin,xmax,epsilon = EPSILON):
  while True:
      xmid = (xmin + xmax) * 0.5
      if (xmax-xmin < epsilon):
        return xmid
      fn_mid = fn(xmid)
      fn_min = fn(xmin)
      if fn_min*fn_mid < 0:
          xmax = xmid
      else:
          xmin = xmid
    
'''
Found something similar at this gamedev question:
https://gamedev.stackexchange.com/questions/11116/kepler-orbit-get-position-on-the-orbit-over-time?newreg=e895c2a71651407d8e18915c38024d50

Equations taken from:
https://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion#Position_as_a_function_of_time
'''
def SolveOrbit(rmax, rmin, t):
  # calculation precision
  epsilon = EPSILON
  # mass of the sun [kg]
  Msun = 1.9891e30
  # Newton's gravitational constant [N*m**2/kg**2]
  G = 6.6740831e-11
  # standard gravitational parameter
  mu = G*Msun
  # eccentricity
  eps = (rmax - rmin) / (rmax + rmin)
  # semi-latus rectum
  p = rmin * (1 + eps)
  # semi/half major axis
  a = p / (1 - eps**2)
  # period
  P = sqrt(a**3 / mu)
  # mean anomaly
  M = (t / P) % (2*pi)
  # eccentric anomaly
  def fn_E(E):
    return M - (E-eps*sin(E))
  E = solve_bisection(fn_E, 0, 2*pi)
  # true anomaly
  # TODO: what if E == pi?
  theta = 2*atan(sqrt((((1+eps)*tan(E/2)**2)/(1-eps))))
  # if we are at the second half of the orbit
  if (E > pi):
    theta = 2*pi - theta
  # heliocentric distance
  r = a * (1 - eps * cos(E))
  return theta, r

def DrawPlanet(name, rmax, rmin, t):
  SCALE = 1e9
  theta, r = SolveOrbit(rmax * SCALE, rmin * SCALE, t)
  x = -r * cos(theta) / SCALE
  y = r * sin(theta) / SCALE
  planet = Circle((x, y), 8)
  ax.add_artist(planet)
    
#These are the arguments taken from hyperphysics.phy-astr.gsu.edu/hbase/solar/soldata2.html
#They are the planet names, max and min distances, and their longitudinal angle
#Also included is Halley's Comet, used to show different scale  and eccentricity
PlanetOrbit('Mercury', 69.8, 46.0)
PlanetOrbit('Jupiter', 741, 817)
PlanetOrbit('Venus', 108.9, 107.5)
PlanetOrbit('Earth', 152.1, 147.1)
PlanetOrbit('Mars', 249.1, 206.7)


# t = time since perihelion in seconds
DrawPlanet('Earth', 152.1, 147.1, 206*60*60*24)
DrawPlanet('Mars', 249.1, 206.7, 77*60*60*24) 
DrawPlanet('Mercury', 69.8, 46.0, 43*60*60*24)
DrawPlanet('Jupiter', 741, 817, 552*60*60*24)
DrawPlanet('Venus', 108.9, 107.5, 16*60*60*24)  

print(60*60*24*365)
  
plt.show()

这两个图像似乎没有明显对齐,所以我尝试了很多方法,例如:仅使用地球的近日点时间,重写SolveOrbit函数,以及使用JPL HORIZONS api找到近日点时间,以防万一它是错误的,这是我无法找到的。

我开始认为问题出在我输入的时间“t”而不是程序,因为我从中得到了它们网站: https://in-the-sky.org/news.php?id=20210612_11_100

也许它不准确,但我不确定还能在哪里找到近日点时间。


在代码中发现了一个难题!虽然它看起来很复杂,但问题在于模拟中行星的起始位置。是对的,问题出在 t (自近日点后的时间)上,但不是找到的值不准确。

假设所有行星的近日点同时发生。 这并非事实。 每个行星都有自己独立的近日点时间,该时间与其轨道周期不同。

以下是更正代码的方法:

  1. 找到每个行星的正确近日点时间: 可以从像 NASA HORIZONS 这样的可靠来源找到这些信息。

  2. 计算自近日点后的时间:

  3. 获取当前日期和时间。
  4. 为每个行星找到自其最近一次近日点以来的持续时间。这将是代码中的 t 值。

  5. 将计算出的 t 应用于每个行星: 在调用 DrawPlanet 时,将每个行星特有的 t 传入。

其他技巧:

  • 验证的单位: 确保的所有单位(时间、距离等)在所有计算中都是一致的(例如,秒、米或天文单位)。
  • 考虑时间跨度: 如果模拟较长的时间跨度,则可能需要考虑岁差和其他长期轨道变化等因素以获得更高的准确性。
  • 逐步调试: 从一个行星开始,验证其在多个时间点的计算位置,然后添加更多行星。

通过更正自近日点以来的时间计算,模拟的准确性应该会提高,并且行星位置应该更接近于 NASA Eyes 等参考点。 祝好运,编码愉快!

标签:python,ellipse,orbital-mechanics
From: 78800541

相关文章

  • 1-python的数据类型与变量
    一、交互模式与脚本模式交互模式:就相当于一种问答模式,关闭即消失无法保存重用比如python自带的编译器脚本模式:可以将代码长期保存以及重复使用如何创建脚本模式:idle——file——newfile[快捷方式:idle——ctrl+n]保存:ctrl+s运行:F5二、变量(Variable)变量:会变化......
  • 2-Python数据类型——序列
    Python数据类型——序列一、序列序列是一个可以存放多个值的容器。有序序列:在序列中每个值都有对应的下标下标:就相当于酒店的房间号,方便客人的查找与酒店的管理在编程中下标的起始值与日常生活中的计数有所不同:下标的计数从0开始计数,从左往右计数:下标从0开始往右递......
  • crontab 运行 .sh 文件调用 python 脚本
    我有一个pythonselenium脚本,可以打开chrome并为我运行一些自动化任务。在crontab中直接调用python可以使用下面的行。:10.0是我运行echo$DISPLAY时得到的值。我使用的是Ubuntu22.04.4LTS5823**2DISPLAY=:10.0/usr/bin/python3/home/user/Script......
  • Pythonanywhere - ping:套接字:不允许操作
    请帮忙。我有一个Telegram机器人,当我从Bash控制台启动他时,它每60秒ping一次静态IP-它工作正常,但每天停止工作一次。我尝试使用“始终开启任务”,但在日志文件中收到“ping:套接字:不允许操作”。我有5美元帐户,我能做什么?从Bash控制台运行时我看到的内容:---17......
  • python+flask计算机毕业设计社区疫情防控物资调配平台(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景近年来,全球公共卫生事件的频发,尤其是新冠疫情的爆发,对社区治理与应急响应能力提出了前所未有的挑战。社区作为疫情防控的第一线,其物资调配......
  • python+flask计算机毕业设计四川工商学院疫情防控系统的设计与实现(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景在全球新冠疫情持续蔓延的背景下,高校作为人员密集、流动性大的特殊场所,其疫情防控工作显得尤为重要。四川工商学院作为一所集教学、科研、......
  • python+flask计算机毕业设计企业人事管理系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着企业规模的不断扩大与业务复杂度的提升,传统的人事管理方式已难以满足现代企业对高效、精准、自动化管理的需求。企业人事管理涉及员工......
  • python+flask计算机毕业设计外卖食品安全监管微信小程序(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着移动互联网技术的飞速发展,外卖行业作为“互联网+餐饮”的典范,近年来呈现出井喷式增长态势,极大地便利了人们的日常生活。然而,外卖食品......
  • python+flask计算机毕业设计楼盘销售系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着房地产市场的蓬勃发展,楼盘销售作为房地产行业的重要环节,其管理效率与服务质量直接影响到企业的市场竞争力和客户满意度。传统的楼盘销......
  • python+flask计算机毕业设计基于web的小区疫情防控信息管理系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着全球公共卫生事件的频发,特别是新冠疫情的持续影响,小区作为城市管理的基础单元,其疫情防控工作显得尤为重要。传统的小区管理方式在面对......