Finding roots of 4th degree polynomial using tensorflow by Halley's method
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Halley’s method is a root-finding algorithm that improves on Newton’s method by using both the first and second derivatives. For a quartic polynomial, it can converge very quickly when the starting guess is reasonable and the denominator in the update stays well-behaved. TensorFlow is useful here because automatic differentiation makes it easy to compute the derivatives without hand-writing every derivative formula.
Halley’s Update Rule
For a function f(x), Halley’s method updates the current estimate with:
Compared with Newton’s method, the extra second-derivative term often gives faster local convergence. The tradeoff is that the denominator can become unstable if the derivatives are small or badly scaled.
For a quartic polynomial, the method works on one root estimate at a time. If you need all four roots, you either run the solver from different starting guesses or deflate the polynomial after finding each root.
Define the Quartic in TensorFlow
A quartic polynomial has the form a*x^4 + b*x^3 + c*x^2 + d*x + e. TensorFlow can evaluate this function and differentiate it automatically.
Using float64 instead of float32 is often a better choice for numerical root finding because the algorithm is sensitive to rounding behavior.
Compute First and Second Derivatives with GradientTape
TensorFlow’s nested gradient tapes make it easy to evaluate f(x), f'(x), and f''(x) in one step.
This avoids manually deriving and coding polynomial derivatives, which is especially nice when the function later becomes more complicated than a plain quartic.
Implement Halley’s Iteration
With the derivatives available, the solver loop is short.
This returns one approximate root and the number of iterations used.
Initial Guesses Matter
Halley’s method is still an iterative local method. A poor initial guess can lead to slow convergence, convergence to an unexpected root, or outright failure if the denominator becomes too small.
That means a practical root-finding workflow often includes:
- plotting or sampling the polynomial to find promising starting regions
- trying several initial guesses
- checking the residual
f(root)after convergence
TensorFlow helps with derivatives, but it does not remove the need for sensible numerical strategy.
Finding More Than One Root
A quartic can have up to four real or complex roots. The code above finds one real root at a time. To find several real roots, run the solver with different starting values and compare the converged results. If repeated guesses converge to the same root, you may need more diverse initial points.
If exact polynomial root extraction is the real goal, specialized algebraic or numerical polynomial solvers are often more direct than building a TensorFlow-based iterative method. TensorFlow is most useful here when the root-finding step is part of a larger differentiable workflow.
Common Pitfalls
- Starting from a poor initial guess and assuming the method should still converge to the desired root.
- Ignoring the denominator term and not checking for near-zero values.
- Using
float32when the problem benefits from higher numerical precision. - Assuming one converged root means all quartic roots have been found.
- Treating TensorFlow as a magic numerical solver instead of validating the residual after convergence.
Summary
- Halley’s method uses first and second derivatives and can converge faster than Newton’s method.
- TensorFlow makes derivative calculation easy through automatic differentiation.
- Use
float64and check the update denominator for numerical stability. - The method finds one root per starting guess, so multiple roots require multiple runs or additional strategy.
- Always validate the final residual instead of trusting the iteration blindly.

