Advertisement
Guest User

Untitled

a guest
Feb 14th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2. def input_coef(): #ввод данных
  3.     print("Введите коэффициенты:")
  4.     a=float(input("a="))
  5.     b=float(input("b="))
  6.     c=float(input("c="))
  7.     d=float(input("d="))
  8.     return a,b,c,d
  9.  
  10. def check(coef): #проверка, что уравнение кубическое
  11.     a=float(coef[0])
  12.     b=float(coef[1])
  13.     c=float(coef[2])
  14.     d=float(coef[3])
  15.     print ("Заданы коэффициенты:", a, b, c, d)
  16.     if abs(a) > 1e-6:
  17.         return a, b, c, d
  18.     else:
  19.         return False
  20.  
  21. def Cardano(coef): #решение уравнения
  22.     a,b,c,d = coef
  23.     #if a == 0:
  24.     #    return 0
  25.     res = []
  26.     p = (3 * a * c - b * b) / 3 * a ** 2
  27.     q  = (2 * math.pow(b, 3) - 9 * a * b * c + 27 * a * a * d) / 27 * math.pow(a, 3)
  28.     D = (q / 2) ** 2 + (p / 3) ** 3
  29.    
  30.     if D < 0:
  31.         if q < 0:
  32.             fi = math.arctg(math.sqrt(-D))
  33.         elif q > 0:
  34.             fi = math.arctg(math.sqrt(-D)) + math.pi
  35.         else:
  36.             fi = math.pi / 2
  37.         y1 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3)
  38.         y2 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 2 * math.pi / 3)
  39.         y3 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 4 * math.pi / 3)
  40.         res.append(y1 - b / 3 * a)
  41.         res.append(y2 - b / 3 * a)
  42.         res.append(y3 - b / 3 * a)
  43.    
  44.     return res
  45.    
  46.  
  47. coef = input_coef()
  48. check = check(coef)
  49. if check != False:
  50.     roots = Cardano(coef)
  51.     print("решение уравнения:", res)
  52. else:
  53.     print("Уравнение не кубическое")
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement