Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def input_coef(): #ввод данных
- print("Введите коэффициенты:")
- a=float(input("a="))
- b=float(input("b="))
- c=float(input("c="))
- d=float(input("d="))
- return a,b,c,d
- def check(coef): #проверка, что уравнение кубическое
- a=float(coef[0])
- b=float(coef[1])
- c=float(coef[2])
- d=float(coef[3])
- print ("Заданы коэффициенты:", a, b, c, d)
- if abs(a) > 1e-6:
- return a, b, c, d
- else:
- return False
- def Cardano(coef): #решение уравнения
- a,b,c,d = coef
- #if a == 0:
- # return 0
- res = []
- p = (3 * a * c - b * b) / 3 * a ** 2
- q = (2 * math.pow(b, 3) - 9 * a * b * c + 27 * a * a * d) / 27 * math.pow(a, 3)
- D = (q / 2) ** 2 + (p / 3) ** 3
- if D < 0:
- if q < 0:
- fi = math.arctg(math.sqrt(-D))
- elif q > 0:
- fi = math.arctg(math.sqrt(-D)) + math.pi
- else:
- fi = math.pi / 2
- y1 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3)
- y2 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 2 * math.pi / 3)
- y3 = 2 * math.sqrt(-p / 3) * math.cos(fi / 3 + 4 * math.pi / 3)
- res.append(y1 - b / 3 * a)
- res.append(y2 - b / 3 * a)
- res.append(y3 - b / 3 * a)
- return res
- coef = input_coef()
- check = check(coef)
- if check != False:
- roots = Cardano(coef)
- print("решение уравнения:", res)
- else:
- print("Уравнение не кубическое")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement