import numpy as np
def solve_quad(a,b,c):
if a == 0:
print("您輸入的不是二次方程!")
else:
delta = b*b-4*a*c
x = -b/(2*a)
if delta == 0:
print("方程有惟一解,X=%f"%(x))
return x
elif delta > 0:
x1 = x-np.sqrt(delta)/(2*a)
x2 = x+np.sqrt(delta)/(2*a)
print("方程有兩個實根:X1=%f,X2=%f"%(x1,x2))
return x1,x2
x1 = (-b+complex(0,1)*np.sqrt((-1)*delta))/(2*a)
x2 = (-b-complex(0,1)*np.sqrt((-1)*delta))/(2*a)
print("方程有兩個虛根,如下所示:")
print(x1,x2)
import numpy as np
def solve_quad(a,b,c):
if a == 0:
print("您輸入的不是二次方程!")
else:
delta = b*b-4*a*c
x = -b/(2*a)
if delta == 0:
print("方程有惟一解,X=%f"%(x))
return x
elif delta > 0:
x1 = x-np.sqrt(delta)/(2*a)
x2 = x+np.sqrt(delta)/(2*a)
print("方程有兩個實根:X1=%f,X2=%f"%(x1,x2))
return x1,x2
else:
x1 = (-b+complex(0,1)*np.sqrt((-1)*delta))/(2*a)
x2 = (-b-complex(0,1)*np.sqrt((-1)*delta))/(2*a)
print("方程有兩個虛根,如下所示:")
print(x1,x2)
return x1,x2