Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - Math domain error when calculating equation

I'm a Python beginner, and I'm writing a code that takes input from the user and uses it to run a calculation until a certain variable reaches 0:

import math

ind = 1
x_i = 1
dt = 0.01
k = 11.5
r_out = 0.889
w = 0.03175
dL = 30
m = float(input("Enter m: "))
v_i = float(input("Enter v_i: "))

t =[0]
x = [x_i]
v = [v_i]
Fc = []

while (v_i > 1):
    Fc_i = (k * v_i ** 2 * math.cos(math.atan(30 / x_i)) / 2 * (math.sqrt(r_out ** 2 - (w / math.pi) * (math.sqrt(x_i ** 2 + dL ** 2)))))
    Fc.append(Fc_i)
    x.append(x_i + v_i * dt + 0.5 * (-Fc_i / m) * dt)
    v.append(v_i - (Fc_i / m))
    t.append(dt * ind)
    print(Fc_i, v_i, x_I)
    ind += 1
    Fc_i = Fc[-1]
    x_i = x[-1]
    v_i = v[-1]

When I enter the two values (in this example, m = 19958.06 and v_i = 69.444), the code runs perfectly fine for the first ~100 loops, but then gives me an error:

    Fc_i = (k * v_i ** 2 * math.cos(math.atan(30 / x_i)) / 2 * (math.sqrt(r_out ** 2 - (w / math.pi) * (math.sqrt(x_i ** 2 + dL ** 2)))))
ValueError: math domain error

I've checked all variable values and tried calculating the equation myself using the formula I've replicated in the code, and I don't run into anything odd. Is there a mistake in the code/the way I pass the variables on towards the next loop?

question from:https://stackoverflow.com/questions/65883900/math-domain-error-when-calculating-equation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue occurs in this part of your calculation

math.sqrt(r_out ** 2 - (w / math.pi) * (math.sqrt(x_i ** 2 + dL ** 2)))

looking at the values when this errors

r_out = 0.889
w = 0.03175
x_i = 72.2987746569653
dL = 30

If we plug these in the calculations and start solving

math.sqrt(0.889 ** 2 - (0.03175 / math.pi) * (math.sqrt(72.2987746569653 ** 2 + 30 ** 2)))
math.sqrt(0.889 ** 2 - (0.03175 / math.pi) * (math.sqrt(5227.112816898649 + 900)))
math.sqrt(0.889 ** 2 - (0.03175 / math.pi) * (math.sqrt(6127.112816898649)))
math.sqrt(0.889 ** 2 - (0.03175 / math.pi) * (78.27587634066225))
math.sqrt(0.889 ** 2 - (0.010106338886335354) * (78.27587634066225))
math.sqrt(0.790321 - (0.010106338886335354) * (78.27587634066225))
#now here multiplaction takes precedence over subtraction
math.sqrt(0.790321 - 0.7910825329236124)
math.sqrt(-0.0007615329236123625)
#This is an error as negative number cannot have a real root.

If you mean for r_out ** 2 - (w / math.pi) to happen before the multiplication then you need to put brackets around it

Fc_i = (k * v_i ** 2 * math.cos(math.atan(30 / x_i)) / 2 * (
    math.sqrt((r_out ** 2 - (w / math.pi)) * (math.sqrt(x_i ** 2 + dL ** 2)))))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...