这是我写的用来计算火箭模型高度的代码。在第49行我收到一个数学域错误消息。以下是指向错误消息的链接:
from math import *
motor_type = raw_input("What is the motor letter")
b4 = { "th" : 13.2, "i" : 5.00, "ti" : 0.8}
b6 = { "th" : 12.1, "i" : 5.00, "ti" : 0.8}
c = { "th" : 15.3, "i" : 10.00, "ti" : 1.6}
d = { "th" : 32.9, "i" : 20.00, "ti" : 1.6}
e = { "th" : 25.0, "i" : 30.00, "ti" : 2.8}
#######################################################
a1 = b4["th"]
a2 = b4["i"]
a3 = b4["ti"]
b1 = b6["th"]
b2 = b6["i"]
b3 = b6["ti"]
c1 = c["th"]
c2 = c["i"]
c3 = c["ti"]
d1 = d["th"]
d2 = d["i"]
d3 = d["ti"]
e1 = e["th"]
e2 = e["i"]
e3 = e["ti"]
#######################################################
def get_altitude(th, i, ti):
Cd = float(.075)#drag coefficient = 0.75 for average rocket
rho = float(1.22)#air density = 1.22 kg/m3
g = float(9.81)#acceleration of gravity = 9.81 m/s2
v = float(0.0)#burnout velocity in m/s
y1 = float(0.0)#altitude at burnout
yc = float(0.0)#coasting distance
ta = float(0.0)#coasting time => delay time for motor
m = float(raw_input("What is the mass in Kg of the rocket with the motor loaded ")) #rocket mass in kg
a = pi * (((float(int(raw_input("What is the diameter of the rocket body in cm")))) / 200)**2) #rocket cross-sectional area in m2
#######################################################
k = rho*Cd*a*0.5
q = (th - m * g) / k
x = ((2*k*q)/ m)
v = q * ((1 - exp(-x * ti))/(1 + exp(-x * ti)))
yc = (m / (2*k)) * log((m*g+k * (v**2))/(m*g))
y1 = (-m/(2*k))*log((th - m * g - k * (v**2)) / (th - (m * g)))
qa = sqrt(m*g/k)
qb = sqrt(g*k/m)
ta = atan(v/qa) / qb
altitude = y1 + yc
print "Time from burnout to apogee"
print ta
print "Max altitude"
print altitude
if motor_type == "b4":
get_altitude(a1, a2, a3)
elif motor_type == "b6":
get_altitude(b1, b2, b3)
elif motor_type == "c":
get_altitude(c1, c2, c3)
elif motor_type == "d":
get_altitude(d1, d2, d3)
elif motor_type == "e":
get_altitude(e1, e2, e3)
在第49行执行
log
操作,但是log
只为大于0的数字定义。所以,你得到了一个数学域错误。检查你的价值观。在作为故意造成这个错误的一个随机例子。。。。在
假设我为马达输入“b4”,输入0.0005,然后输入1,分别表示质量(单位:kg)和直径(单位:cm)
log
中表达式的值将为-3.6*10^6,并且未定义该值的对数。在另外,我还注意到了一个小问题,就是不需要为变量做
float(0.0)
代码,0.0已经是一个浮点数了。同样,对于其他浮动。在相关问题 更多 >
编程相关推荐