本文介绍了二次方程求解器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建二次方程求解器,但是当我输入大于1的系数时,它似乎不起作用?代码和错误消息如下.任何帮助,我们将不胜感激.
I'm trying to create a quadratic equation solver but it doesn't seem to be working when I put in a coefficient greater than 1? The code and error message is below. Any help is greatly appreciated.
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
推荐答案
部分代码:
#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.
If D
is less than 0, this lines will raise ValueError
you got. As error message said number cannot be raised to a fractional power. So we have to check if D
is less than 0 or not.
#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
这篇关于二次方程求解器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!