这是我为计算模型火箭的高度而编写的代码。在第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)
答案 0 :(得分:2)
在第49行,您正在执行log
操作,但log
仅针对大于0的数字定义。因此,您会收到数学域错误。检查你的价值观。
作为故意造成此错误的随机示例......
假设我输入“b4”表示电机输入0.0005然后输入1表示质量(千克)和直径(厘米)分别表示log
中的表达式值为-3.6 * 10 ^ 6这个日志没有定义。
另外,我注意到的一件小事就是为你的变量做float(0.0)
的代码而不必要0.0是一个浮点数。同样,对于其他花车。