我遇到了一个问题,即一颗卫星在距离地球中心p = 15000km的高度绕地球运行。我需要使用方程 r = p/1-e*cos(theta( 创建一个从地球表面与 θ 从 0 - 360 到 θ 的表格,以 30 度为增量,其中 e 表示偏心率,p 为 15000 公里。我得到 e = 0, 0.2, 0.7。执行以下操作会导致浮点错误。我猜这是因为我试图用整数绘制 theta 的数字列表,但我不知道如何解决它。
pi = 3.14159
eccentricity = 0
theta = pi/6, pi/4, pi/3, pi/2, 2*pi/3, 3*pi/4, 5*pi/6, pi, 7*pi/6, 5*pi/4, 4*pi/3, 3*pi/2, 5*pi/3, 7*pi/4, 11*pi/6, 2*pi
p = 8629 #15000 - radius of Earth in km
def r(eccentricity, p, theta):
return p/(1-eccentricity*math.cos(theta))
print r(eccentricity, p, theta)
由于theta
是浮点数的元组,因此不能将其传递给需要单个浮点数的函数。
您有两种选择:
使用列表推导式计算高度:
heights = [r(eccentricity, p, th) for th in theta]
使用允许直接使用数组的numpy
import numpy as np
theta = np.array(theta)
heights = r(eccentricity, p, theta)
编辑:由于您使用的是numpy,因此您可以使用简化theta
数组
theta = np.deg2rad(np.arange(0, 360, 30))