Решатель квадратных уравнений не работает

Я пытаюсь создать решатель квадратного уравнения, но он не работает, когда я ввожу коэффициент больше 1? Код и сообщение об ошибке ниже. Любая помощь приветствуется.

print "Welcome to the quadratic equation solver."
print "The general quadratic equation = ax^2 + bx + c.\n"

def POS(a,b):
#This function gives the point at which the quadratic turns
    tp = float((-b)/(a*2))
    return (tp)

#This allows for the user to input the values of the variables
while True:
    s = raw_input ("Please insert a numerical value for a: ")
    try:
        a = float(s)
        break
    except ValueError:
        print "Please enter a numeric value."
print ("Well done.\n")

while True:
    s = raw_input ("Please insert a numerical value for b: ")
    try:
        b = float(s)
        break
    except ValueError:
        print "Please enter a numeric value."
print ("Well done.\n")

while True:
    s = raw_input ("Please insert a numerical value for c: ")
    try:
        c = float(s)
        break
    except ValueError:
        print "Please enter a numeric value."
print ("Well done.\n")

#This uses the function to give the co-ordinate of the turning point
print POS(a,b), "is the x-value of the turning point"
print ((a)*(POS(a,b)**2))+((b)*POS(a,b))+c, "is they y-value of the turning point. \n"

#This tells whether the quadratic is positive or negative
if a >0:
    print "The quadratic is positive.\n"

if a<0:
    print "The quadratic is negative.\n"

#This determines the root of the quadratic
root1 = (-b +((b**2) - (4*a*c))**0.5) / (2 * a)
root2 = (-b -((b**2) - (4*a*c))**0.5) / (2 * a)
print "The quadratic has a root at x =",root1
if root1 != root2:
    print "The quadratic has another root at x =",

#This uses the discriminant to determine the nature of the quadratic
if (b**2)-(4*a*c) == 0:
    print root1

elif (b**2)-(4*a*c) > 0:
    print root1 and root2

elif (b**2)-(4*a*c) < 0:
    print "The quadratic contains no roots"

for x in range(-1000,1000):
    quadratic = (a*x**2)+(b*x)+c

#Determines the derivitive and second derivitive of the quadratic
print "The derivitive of the quadratic is: ",2*a,"x", "+",b
print "The second derivitive of the quadratic is"

#Create plot
X = arange (-1000,1000,1)
plot(X, quadratic, linewidth=3, label="quadratic")

#Create title and axes label
title ("Graph of the quadratic")
xlabel ("x")
ylabel ("y")

#grid
grid(True)
legend()
show()

Сообщение об ошибке:

Traceback (most recent call last):
  File "C:\Users\Peter\Downloads\test.py", line 49, in <module>
    root1 = (-b +((b**2) - (4*a*c))**0.5) / (2 * a)
ValueError: negative number cannot be raised to a fractional power

person Peter Strife    schedule 17.05.2015    source источник
comment
Арифметика с плавающей запятой неточная. Возможно, (b**2) - (4*a*c) отрицательный.   -  person Peter Wood    schedule 17.05.2015
comment
Был бы способ заставить программу не показывать корней вместо сообщения об ошибке/   -  person Peter Strife    schedule 17.05.2015
comment
почему бы вам не выполнить тест if (b**2)-(4*a*c) > 0 перед вычислением корней?   -  person Julien Spronck    schedule 17.05.2015
comment
Проблема в том, что b**2 - 4*a*c может привести к очень маленькому отрицательному числу, когда оно должно быть равно нулю. Это не значит, что уравнение не имеет корней.   -  person TigerhawkT3    schedule 17.05.2015
comment
Не редактируйте вопрос, кроме небольших правок, таких как опечатки или небольшие дополнения. Если необходимо, удалите его, но лучше оставить его в StackOverflow навсегда.   -  person pmg    schedule 18.05.2015
comment
Вы можете использовать cmath.sqrt для получения сложного корня.   -  person Peter Wood    schedule 21.05.2015


Ответы (4)


Вы также можете переключиться на Python3, чтобы вместо ValueError ваш номер неявно преобразовывался в комплексное число.

(-2)**.2
(0.9293164906031477+0.6751879523998812j)
person AlexLordThorsen    schedule 17.05.2015

Часть вашего кода:

#This determines the root of the quadratic
root1 = (-b +((b**2) - (4*a*c))**0.5) / (2 * a)
root2 = (-b -((b**2) - (4*a*c))**0.5) / (2 * a)
print "The quadratic has a root at x =",root1
if root1 != root2:
    print "The quadratic has another root at x =",

#This uses the discriminant to determine the nature of the quadratic
if (b**2)-(4*a*c) == 0:
    print root1

elif (b**2)-(4*a*c) > 0:
    print root1 and root2

elif (b**2)-(4*a*c) < 0:
    print "The quadratic contains no roots"

for x in range(-1000,1000):
    quadratic = (a*x**2)+(b*x)+c

Заменим (b**2)-(4*a*c) на D:

#This determines the root of the quadratic
D = (b**2)-(4*a*c)

root1 = (-b +(D)**0.5) / (2 * a)
root2 = (-b -(D)**0.5) / (2 * a)

print "The quadratic has a root at x =", root1
if root1 != root2:
    print "The quadratic has another root at x =",

#This uses the discriminant to determine the nature of the quadratic
if D == 0:
    print root1

elif D > 0:
    # old code:
    # print root1 and root2
    # new code:
    print root1, root2

elif D < 0:
    print "The quadratic contains no roots"

for x in range(-1000,1000):
    quadratic = (a*x**2)+(b*x)+c

Проблема в этих двух строках:

root1 = (-b +(D)**0.5) / (2 * a)
root2 = (-b -(D)**0.5) / (2 * a)

Если D меньше 0, эти строки поднимут ValueError, которые вы получили. В сообщении об ошибке указанное число нельзя возвести в дробную степень. Итак, мы должны проверить, меньше ли D 0 или нет.

#This determines the root of the quadratic
D = (b**2)-(4*a*c)

# new code:
if D >= 0:
    root1 = (-b +(D)**0.5) / (2 * a)
    root2 = (-b -(D)**0.5) / (2 * a)

    print "The quadratic has a root at x =", root1
    if root1 != root2:
        print "The quadratic has another root at x =", root2


#This uses the discriminant to determine the nature of the quadratic
# We've already printed root1 and root2
# if D == 0:
#     print root1

# elif D > 0:
#     print root1, root2

# D < 0
else:
    print "The quadratic contains no roots"

for x in range(-1000,1000):
    quadratic = (a*x**2)+(b*x)+c
person f43d65    schedule 17.05.2015

#This uses the discriminant to determine the nature of the quadratic
if (b**2)-(4*a*c) == 0:
    print root1

elif (b**2)-(4*a*c) > 0:
    print root1 and root2

elif (b**2)-(4*a*c) < 0:
    print "The quadratic contains no roots"

Выполните эту проверку перед обработкой корней и разбивкой, если это сложный корень.

>>> a=2
>>> b=4
>>> c=1
>>> delta=math.pow(b,2)-4*a*c
>>> math.sqrt(delta) #raises error for -ve integers
3.4641016151377544 
gamma=math.sqrt(delta)
>>>root1=(-b+gamma)/2/a
>>>root2=(-b-gamma)/2/a
person ahumblenerd    schedule 17.05.2015

Я думаю, что проблема с вашим кодом вызвана следующей частью:

#This determines the root of the quadratic
root1 = (-b +((b**2) - (4*a*c))**0.5) / (2 * a)
root2 = (-b -((b**2) - (4*a*c))**0.5) / (2 * a)
print "The quadratic has a root at x =",root1
if root1 != root2:
    print "The quadratic has another root at x =",

#This uses the discriminant to determine the nature of the quadratic
if (b**2)-(4*a*c) == 0:
    print root1

elif (b**2)-(4*a*c) > 0:

    print root1 and root2

elif (b**2)-(4*a*c) < 0:
    print "The quadratic contains no roots"

Любое отрицательное число, возведенное в дробную степень, вернет комплексное число, которое компьютер не сможет вычислить.

В (b**2) - (4*a*c))**0.5 есть вероятность, что b**2 меньше 4*a*c. В таком случае произойдет ValueError.

Чтобы предотвратить это: вы должны структурировать свой код следующим образом:

if (b**2)-(4*a*c) == 0:
    print root1

elif (b**2)-(4*a*c) > 0:
    root1 = (-b +((b**2) - (4*a*c))**0.5) / (2 * a)
    root2 = (-b -((b**2) - (4*a*c))**0.5) / (2 * a)
    print "The quadratic has a root at x =",root1
    if root1 != root2:
       print "The quadratic has another root at x =",
    print root1 and root2

elif (b**2)-(4*a*c) < 0:
    print "The quadratic contains no roots"    
person AkaiShuichi    schedule 17.05.2015