-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunc
37 lines (29 loc) · 1.05 KB
/
Func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Fun3__ 111
def cubic_root(a, b, c, d):
"""
Find the roots of a cubic equation of the form ax^3 + bx^2 + cx + d = 0.
Parameters:
- a, b, c, d: Coefficients of the cubic equation.
Returns:
- roots: List containing the roots of the equation.
"""
# Calculate the discriminant and other coefficients
delta0 = b**2 - 3 * a * c
delta1 = 2 * b**3 - 9 * a * b * c + 27 * a**2 * d
C = ((delta1 + (delta1**2 - 4 * delta0**3)**0.5) / 2)**(1/3)
# Calculate the roots
root1 = -1 / (3 * a) * (b + C + delta0/C)
root2 = -1 / (3 * a) * (b + (complex(-0.5, 3**0.5 / 2) * C) + delta0 / (complex(-0.5, 3**0.5 / 2) * C))
root3 = -1 / (3 * a) * (b + (complex(-0.5, -3**0.5 / 2) * C) + delta0 / (complex(-0.5, -3**0.5 / 2) * C))
return [root1, root2, root3]
# Example usage:
if __name__ == "__main__":
# Example coefficients of the cubic equation ax^3 + bx^2 + cx + d = 0
a = 1
b = -6
c = 11
d = -6
# Find the roots
roots = cubic_root(a, b, c, d)
# Print the result
print(f"Roots: {roots}")