Advertisement
Guest User

Untitled

a guest
Feb 14th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  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(a, b, c, d): #решение уравнения
  22. #if a == 0:
  23. # return 0
  24. res = []
  25. p = (3 * a * c - b * b) / 3 * a ** 2
  26. q = (2 * math.pow(b, 3) - 9 * a * b * c + 27 * a * a * d) / 27 * math.pow(a, 3)
  27. D = (q / 2) ** 2 + (p / 3) ** 3
  28.  
  29. if D < 0:
  30. if q < 0:
  31. fi = math.arctg(math.sqrt(-D))
  32. elif q > 0:
  33. fi = math.arctg(math.sqrt(-D)) + math.pi
  34. else:
  35. fi = math.pi / 2
  36. y1 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3)
  37. y2 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 2 * math.pi / 3)
  38. y3 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 4 * math.pi / 3)
  39. res.append(y1 - b / 3 * a)
  40. res.append(y2 - b / 3 * a)
  41. res.append(y3 - b / 3 * a)
  42.  
  43. return res
  44.  
  45.  
  46. coef = input_coef()
  47. check = check(coef)
  48. if check != False:
  49. roots = Cardano(input_coef)
  50. print("решение уравнения:", res)
  51. else:
  52. print("Уравнение не кубическое")
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement