Hi,
I'm responding to a post about finding roots of a cubic or quartic equation non-iteratively. One obviously could create functions using the explicit algebraic solutions. One post on the subject noted that the square-roots in those solutions also require iteration, and one post claimed iterative solutions are more accurate than the explicit solutions.
This post, however, is about comparative accuracy of (1) the R solve functionused in the included post, (2) the R polyroot function, (3) the Matlab roots function, (4) the SAS IML polyroot function. I tried the posted polynomial:
-8 + 14*x - 7*x^2 + x^3 = 0
and a repeating-roots example:
8 - 36*x + 54*x^2 - 27*x^3 = 0
I used Mathematica solutions as the reference:
(* Posted example *)
Roots[-8 + 14 x - 7 x^2 + x^3 == 0, x]
x == 1 || x == 2 || x == 4
(* Repeating-roots example *)
Roots[8 - 36 x + 54 x^2 - 27 x^3 == 0, x]
x == 2/3 || x == 2/3 || x == 2/3
The results indicate that the R polyroot function is the most accurate for these examples. The R solve function is quite inaccurate for the repeating-roots example. It appears to be single-precision arithmetic on the real and imaginary parts. The same appears to be true for the Matlab function roots. SAS IML polyroot function appears to use double-precision calculations but was not nearly as accurate as the R polyroot function for the repeating-roots example.
The syntax and output for these examples are as follows:
# ------------------------------------------------------------------------- #
Mathematica:
(* Posted example *)
Roots[-8 + 14 x - 7 x^2 + x^3 == 0, x]
x == 1 || x == 2 || x == 4
(* Repeating-roots example *)
Roots[8 - 36 x + 54 x^2 - 27 x^3 == 0, x]
x == 2/3 || x == 2/3 || x == 2/3
# ------------------------------------------------------------------------- #
R: