For a cubic polynomial, there are closed-form solutions , but they are not particularly well suited for numerical calculus.
I would do the following for the cubic case: any cubic polynomial has at least one real root, you can easily find it using the Newton method. Then you use deflation to get the remaining quadratic polynomial to see my answer there for the correct completion of this last step.
One word of caution: if the discriminant is close to zero, there will be a numerically multiple real root, and the Newton method will fail. Moreover, since the polynomial in the neighborhood of the root is similar to (x - x0) ^ 2, you will lose half of your significant digits (since P (x) will be <epsilon as soon as x - x0 <sqrt (epsilon)). Therefore, you can exclude this and use a closed-form solution in this particular case or solve a derived polynomial.
If you want to find roots in a given interval, check Sturm's theorem .
A more general (complex) algorithm for solving a general polynomial is the Jenkins-Traub algorithm . This is clearly redundant, but works well on cubes. Usually you use a third-party implementation.
Since you are using C, using GSL is definitely the best choice.
Another common method is to search for eigenvalues โโof the companion matrix using, for example. Balanced QR Decomposition or Reduction to Homeowner Form. This is the approach taken by GSL.
Alexandre C.
source share