Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt, acos, cos, pi
- # Coefficients of x^3 + (C_1)x^2 + (C_2)x + C_3 = 0
- C = [1, # Coefficient C_1
- 1, # Coefficient C_2
- 1 # Coefficient C_3
- ]
- # Substitutions (see solution of Cubic equations:
- # mathworld.wolfram.com/CubicFormula.html )
- w = (3*C[1] - C[0]**2)/3.0
- q = (27*C[2] - 9*C[0]*C[1] + 2*C[0]**3)/27.0
- R_t = (w/3.0)**3 + (q/3.0)**2.0
- q_s = q/abs(q) #math.copysign(1, q)
- if R_t < 0:
- Ratio = sqrt(((q/2.0)**2)/(-(w/3.0)**3))
- if abs(Ratio) < 1.0:
- phi = acos(Ratio)
- else:
- raise ValueError # Raise Math error if no solution
- #phi = math.degrees(math.acos(Ratio-2)+math.pi)
- # math.degrees(math.acos(Ratio-2))
- else:
- raise ValueError # Add this to your exception handling for when a solution does not exist.
- # Analytical expressions for roots:
- roots = [-2*q_s*sqrt(-w/3.0)*cos(phi/3.0 ) - C[0]/3.0,
- -2*q_s*sqrt(-w/3.0)*cos(phi/3.0 + 2*pi/3.0) - C[0]/3.0,
- 2*q_s*sqrt(-w/3.0)*cos(phi/3.0 + 4*pi/3.0) - C[0]/3.0
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement