Advertisement
wachiorsino

CUBEROOT nasa.gov

Mar 1st, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function cuberoot,cc
  2. ;+
  3. ; NAME:
  4. ; CUBEROOT
  5. ; PURPOSE:
  6. ; Real roots of a cubic equation. Complex roots set to -1.0e30.
  7. ; Called by HALFAGAUSS.
  8. ; CALLING SEQUENCE:
  9. ; roots = CUBEROOT(cc)
  10. ; INPUT:
  11. ; cc = 4 element vector, giving coefficients of cubic polynomial,
  12. ; [c0,c1,c2,c3], where one seeks the roots of
  13. ; c0 + c1*x + c2*x^2 + c3*x^3
  14. ; OUTPUT:
  15. ; Function returns a vector of 3 roots. If only one root is real, then
  16. ; that becomes the 1st element.
  17. ; EXAMPLE:
  18. ; Find the roots of the equation
  19. ; 3.2 + 4.4*x -0.5*x^2 -x^3 = 0
  20. ; IDL> x = cuberoot([3.2,4.4,-0.5,-1])
  21. ;
  22. ; will return a 3 element vector with the real roots
  23. ; -1.9228, 2.1846, -0.7618
  24. ; REVISION HISTORY:
  25. ; Henry Freudenreich, 1995
  26. ; Ensure constants are double precision W. Landsman Jan 2007
  27. ;-
  28.  
  29. on_error,2
  30. pi2 = 2*!DPI
  31. onethird = 1/3.0d
  32. unreal=-1.0e30
  33.  
  34. a1=cc(2)/cc(3)
  35. a2=cc(1)/cc(3)
  36. a3=cc(0)/cc(3)
  37.  
  38. q=(a1^2-3.*a2)/9. & r=(2.*a1^3-9.*a1*a2+27.*a3)/54.
  39.  
  40. if r^2 lt q^3 then begin
  41. ; 3 real roots.
  42. theta=acos(r/q^1.5)
  43. x1=-2.*sqrt(q)*cos(theta/3.)-a1/3.
  44. x2=-2.*sqrt(q)*cos((theta+ pi2)/3.)-a1/3.
  45. x3=-2.*sqrt(q)*cos((theta-pi2)/3.)-a1/3.
  46. endif else begin
  47. ; Get the one real root:
  48. a=-r/abs(r) * (abs(r)+sqrt(r^2-q^3))^onethird
  49. if a eq 0. then b=0. else b=q/a
  50. x1=(a+b)-a1/3.
  51. x2=unreal
  52. x3=unreal
  53. endelse
  54.  
  55. roots=[x1,x2,x3]
  56. return,roots
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement