python解一元二次方程虚根_怎么用python解一元二次方程

  • Post author:
  • Post category:python


ppt88.png

import numpy as npdef 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

改其他的函数测试

12345678910111213141516171819202122import 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