绘制椭圆轨道 [英] Plotting elliptical orbits

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

问题描述

我正在尝试编写一个代码,使用椭圆r = a(1-e ^ 2)/(1 + e * cos(theta))的方程式绘制对象的椭圆路径.我也希望将这些数据放入数组以供其他使用.

I'm trying to write a code that plots the elliptical paths of an object using the equation for the ellipse r=a(1-e^2)/(1+e*cos(theta)). I'd also like this data to be put into an array for other use.

from numpy import *#Imports Python mathematical functions library
import matplotlib.pyplot as plt #Imports plot library
from pylab import *

a = 5
e = 0.3
theta = 0
while theta <= 2*pi:
    r = (a*(1-e**2))/(1+e*cos(theta))
    print("r = ",r,"theta = ",theta)
    plt.polar(theta, r)
    theta += pi/180

plt.show()

代码会给出r和theta的正确值,但是该图为空白.极坐标图窗口出现,但未绘制任何图.

The code spits out correct values for r and theta, but the plot is blank. The polar plot window appears, but there is nothing plotted.

请帮助.预先感谢.

推荐答案

不要为每个点一次调用 plt.polar .相反,调用一次,将所有数据作为输入:

Do not call plt.polar once for every point. Instead, call it once, with all the data as input:

import numpy as np #Imports Python mathematical functions library
import matplotlib.pyplot as plt #Imports plot library
cos = np.cos
pi = np.pi

a = 5
e = 0.3
theta = np.linspace(0,2*pi, 360)
r = (a*(1-e**2))/(1+e*cos(theta))
plt.polar(theta, r)

print(np.c_[r,theta])

plt.show()

顺便说一句,numpy 可以将计算作为两行,而不是使用 while 循环:

By the way, numpy can do the calculation as a two-liner, instead of using a while-loop:

theta = np.linspace(0,2*pi, 360)   # 360 equally spaced values between 0 and 2*pi
r = (a*(1-e**2))/(1+e*cos(theta))  

这将 thetar 定义为 numpy 数组(而不是单个值).

This defines theta and r as numpy arrays (rather than single values).

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

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