What is a simple way to find real roots of a (cubic) polynomial?
Asked Answered
H

5

7

this seems like an obvious question to me, but I couldn't find it anywhere on SO. I have a cubic polynomial and I need to find real roots of the function. What is THE way of doing this?

I have found several closed form formulas for roots of a cubic function, but all of them use either complex numbers or lots of goniometric functions and I don't like them (and also don't know which one to choose).

I need something simple; faster is better; and I know that I will eventually need to solve polynomials of higher order, so having a numerical solver would maybe help too. I know I could use some library to do the hard work for me, but lets say I want to do this as an exercise.

I'm coding in C, so no import magic_poly_solver, please.

Bonus question: How do I find only roots inside a given interval?

Horseplay answered 5/2, 2011 at 11:20 Comment(0)
L
10

For a cubic polynomial there are closed form solutions, but they are not particularly well suited for numerical calculus.

I'd do the following for the cubic case: any cubic polynomial has at least one real root, you can find it easily with Newton's method. Then, you use deflation to get the remaining quadratic polynomial to solve, see my answer there for how to do this latter step correctly.

One word of caution: if the discriminant is close to zero, there will be a numerically multiple real root, and Newton's method will miserably fail. Moreover, since to the vicinity of the root, the polynomial is like (x - x0)^2, you'll lose half your significant digits (since P(x) will be < epsilon as soon as x - x0 < sqrt(epsilon)). So you may want to rule this out and use the closed form solution in this particular case, or solve the derivative polynomial.

If you want to find roots in a given interval, check Sturm's theorem.

A more general (complex) algorithm for generic polynomial solving is Jenkins-Traub algorithm. This is clearly overkill here, but it works well on cubics. Usually, you use a third-party implementation.

Since you do C, using the GSL is surely your best bet.

Another generic method is to find the eigenvalues of the companion matrix with eg. balanced QR decomposition, or reduction to Householder form. This is the approach taken by GSL.

Loppy answered 5/2, 2011 at 12:11 Comment(2)
Thanks for the answer, but I have one more question: Where do I get the first estimate for Newton's method, should I just put 0 in?Horseplay
@cube: good point. Put 0, if it doesn't work, put 1. You can also solve for the derivative polynomial to get the variations of the cubic. If there is only 1 root, 0 will do, if there are 3, start with any number between the roots of the derivative polynomial.Loppy
P
3

For solving cubic equations with simple C code, I have found the QBC solver by noted numerics expert professor William Kahan to be sufficiently robust, reasonably fast and reasonably accurate:

William Kahan, "To solve a real cubic equation." PAM-352, Center for Pure and Applied Mathematics, University of California, Berkely. November 10, 1986. (online, online)

This uses a derivative-based iterative method to find the real root, reduces to a quadratic equation based on that, finally uses a numerically robust quadratic equation solver to find the two remaining roots. Typically, the iterative solver requires about five to ten iterations to converge to the result. Both solvers can be enhanced for accuracy and performance by judicious use of fused multiply-add (FMA) operations, available in ISO C99 via the fma() standard math function.

Crucial to the accuracy of the quadratic solver is the computation of the discriminant. For this, I use the following code based on recent research:

/* Compute B*B - A*C, accurately 

   Claude-Pierre Jeannerod, Nicolas Louvet, and Jean-Michel Muller, 
   "Further Analysis of Kahan's Algorithm for the Accurate Computation 
   of 2x2 Determinants". Mathematics of Computation, Vol. 82, No. 284, 
   Oct. 2013, pp. 2245-2264

   https://www.ams.org/journals/mcom/2013-82-284/S0025-5718-2013-02679-8/S0025-5718-2013-02679-8.pdf
*/
double DISC (double A, double B, double C)
{
    double w = C * A;
    double e = fma (-C, A, w);
    double f = fma (B, B, -w);
    double r = f + e;
    return r;
}

Using double-precision arithmetic, Kahan's solver cannot always produce result accurate to double precision. One of the test cases provided in Kahan's paper illustrates why this is the case:

658x³ - 190125x² + 18311811x - 587898164

Using an arbitrary precision math library, we find that the roots of this cubic equation are as follows:

96.229639346592182_18...
96.357064825184152_07... ± i * 0.069749752043689625_43...

QBC using double-precision arithmetic computes the roots as

96.2296393 50445893
96.35706482 3257289 ± i * 0.0697497 48521837268

The reason for this is that the function evaluation around the real root suffers from errors as large as 60% in the computed function value, preventing the iterative solver from getting closer to the root. By changing the function and derivative evaluation to use double-double computation for intermediate computation (at hefty computational cost), we can address that issue.

/* Data type for double-double computation */
typedef struct {
    double l;  // low / tail
    double h;  // high / head
} dbldbl;

dbldbl make_dbldbl (double head, double tail);
double get_dbldbl_head (dbldbl a);
double get_dbldbl_tail (dbldbl a);
dbldbl add_dbldbl (dbldbl a, dbldbl b);
dbldbl mul_dbldbl (dbldbl a, dbldbl b);

void EVAL (double X, double A, double B, double C, double D, 
           double * restrict Q, double * restrict Qprime, 
           double * restrict B1, double * restrict C2)
{
#if USE_DBLDBL_EVAL
    dbldbl AA, BB, CC, DD, XX, AX, TT, UU;
    AA = make_dbldbl (A, 0);
    BB = make_dbldbl (B, 0);
    CC = make_dbldbl (C, 0);
    DD = make_dbldbl (D, 0);
    XX = make_dbldbl (X, 0);
    AX = mul_dbldbl (AA, XX);
    TT = add_dbldbl (AX, BB);
    *B1 = get_dbldbl_head (TT) + get_dbldbl_tail(TT);
    UU = add_dbldbl (mul_dbldbl (TT, XX), CC);
    *C2 = get_dbldbl_head (UU) + get_dbldbl_tail(UU);
    TT = add_dbldbl (mul_dbldbl (add_dbldbl (AX, TT), XX), UU);
    *Qprime = get_dbldbl_head (TT) + get_dbldbl_tail(TT);
    UU = add_dbldbl (mul_dbldbl (UU, XX), DD);
    *Q = get_dbldbl_head (UU) + get_dbldbl_tail(UU);
#else // USE_DBLDBL_EVAL
    *B1 = fma (A, X, B); 
    *C2 = fma (*B1, X, C);
    *Qprime = fma (fma (A, X, *B1), X, *C2);
    *Q = fma (*C2, X, D);
#endif // USE_DBLDBL_EVAL
}

/* Construct new dbldbl number. |tail| must be <= 0.5 ulp of |head| */
dbldbl make_dbldbl (double head, double tail)
{
    dbldbl z;
    z.l = tail;
    z.h = head;
    return z;
}

/* Return the head of a double-double number */
double get_dbldbl_head (dbldbl a)
{
    return a.h;
}

/* Return the tail of a double-double number */
double get_dbldbl_tail (dbldbl a)
{
    return a.l;
}

/* Add two dbldbl numbers */
dbldbl add_dbldbl (dbldbl a, dbldbl b)
{
    dbldbl z;
    double e, q, r, s, t, u;

    /* Andrew Thall, "Extended-Precision Floating-Point Numbers for GPU 
       Computation." 2006. http://andrewthall.org/papers/df64_qf128.pdf
    */
    q = a.h + b.h;
    r = q - a.h;
    t = (a.h + (r - q)) + (b.h - r);
    s = a.l + b.l;
    r = s - a.l;
    u = (a.l + (r - s)) + (b.l - r);
    t = t + s;
    s = q + t;
    t = (q - s) + t;
    t = t + u;
    z.h = e = s + t;
    z.l = (s - e) + t;

    /* For result of zero or infinity, ensure that tail equals head */
    if (isinf (s)) {
        z.h = s;
        z.l = s;
    }
    if (z.h == 0) {
        z.l = z.h;
    }

    return z;
}

/* Multiply two dbldbl numbers */
dbldbl mul_dbldbl (dbldbl a, dbldbl b)
{
    dbldbl z;
    double e, s, t;

    s = a.h * b.h;    
    t = fma (a.h, b.h, -s);
    t = fma (a.l, b.l, t);
    t = fma (a.h, b.l, t);
    t = fma (a.l, b.h, t);
    z.h = e = s + t;
    z.l = (s - e) + t;

    /* For result of zero or infinity, ensure that tail equals head */
    if (isinf (s)) {
        z.h = s;
        z.l = s;
    }
    if (z.h == 0) {
        z.l = z.h;
    }

    return z;
}

The roots computed with the more accurate function and derivative evaluation are:

96.22963934659218 0
96.35706482518415 3 ± i * 0.06974975204 5672006

While the real parts are now accurate to within the limits of double precision, the imaginary parts are still off. The reason for this is is that in this case the quadratic equation is sensitive to minute differences in the coefficients. A one ulp error in either of them can cause differences of around 10-11 in the imaginary part. This could be worked around by representing the coefficients to higher than double precision and using higher-precision computation in the quadratic solver.

Primacy answered 25/10, 2019 at 22:1 Comment(0)
W
2

If you don't want to use the closed from solutions (or expect polynoms of larger order), the most obvious method would be to calculate approximate roots by using Newton's method.

Unfortunately it's not possible to decide which roots you will get when iterating, although it depends on the starting value.

Also see here.

Wingfield answered 5/2, 2011 at 11:59 Comment(1)
One word of caution though: Newton's method fails miserably with multiple (or numerically multiple) roots. Moreover, once you have one root, you have to remove it from the polynomial, which can be unstable.Loppy
W
2

See Solving quartics and cubics for graphics by D Herbison-Evans, published in Graphics Gems V.

Wilbert answered 5/2, 2011 at 13:22 Comment(2)
Link to the article is dead.Fanlight
@DKrueger, found another link. Thanks.Wilbert
G
0
/*******************************************************************************
 * FindCubicRoots solves:
 *      coeff[3] * x^3 + coeff[2] * x^2 + coeff[1] * x + coeff[0] = 0
 *  returns:
 *      3 - 3 real roots
 *      1 - 1 real root (2 complex conjugate)
 *******************************************************************************/

int FindCubicRoots(const FLOAT coeff[4], FLOAT x[3]);

http://www.realitypixels.com/turk/opensource/index.html#CubicRoots

Gleesome answered 8/5, 2016 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.