gpt4 book ai didi

python - 是什么导致了数学域错误?

转载 作者:行者123 更新时间:2023-12-01 03:59:14 24 4
gpt4 key购买 nike

这是我编写的用于计算模型火箭高度的代码。在第 49 行,我收到一条数学域错误消息。这是错误消息的链接:

1

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 的数字定义。因此,您会收到数学域错误。检查你的值(value)观。

作为故意导致此错误的一个随机示例......

假设我为电机输入“b4”,输入 0.0005,然后分别输入 1 表示质量(以千克为单位)和直径(以厘米为单位),log 中的表达式值为 -3.6 * 10^ 6 并且其日志未定义。

此外,我注意到的一件小事只是掩盖了对变量执行 float(0.0) 的代码是不必要的,0.0 已经是 float 了。同样,对于其他 float 。

关于python - 是什么导致了数学域错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36903295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com