TensorFlow
Halley's Method
Polynomial Roots
4th Degree Polynomial
Numerical Methods

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:

text
x_next = x - (2 * f(x) * f'(x)) / (2 * (f'(x) ** 2) - f(x) * f''(x))

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.

python
1import tensorflow as tf
2
3coefficients = {
4    "a": tf.constant(1.0, dtype=tf.float64),
5    "b": tf.constant(-3.0, dtype=tf.float64),
6    "c": tf.constant(-7.0, dtype=tf.float64),
7    "d": tf.constant(27.0, dtype=tf.float64),
8    "e": tf.constant(-18.0, dtype=tf.float64),
9}
10
11def quartic(x):
12    a = coefficients["a"]
13    b = coefficients["b"]
14    c = coefficients["c"]
15    d = coefficients["d"]
16    e = coefficients["e"]
17    return a * x**4 + b * x**3 + c * x**2 + d * x + e

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.

python
1def derivatives(x_value):
2    x = tf.Variable(x_value, dtype=tf.float64)
3
4    with tf.GradientTape() as outer_tape:
5        with tf.GradientTape() as inner_tape:
6            y = quartic(x)
7        first = inner_tape.gradient(y, x)
8    second = outer_tape.gradient(first, x)
9
10    return y, first, second

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.

python
1def halley_root(initial_guess, max_steps=30, tolerance=1e-12):
2    x = tf.constant(initial_guess, dtype=tf.float64)
3
4    for step in range(max_steps):
5        fx, f1, f2 = derivatives(x)
6        denominator = 2.0 * tf.square(f1) - fx * f2
7
8        if tf.abs(denominator).numpy() < 1e-14:
9            raise ValueError("Halley denominator became too small")
10
11        delta = (2.0 * fx * f1) / denominator
12        x_next = x - delta
13
14        if tf.abs(x_next - x).numpy() < tolerance:
15            return x_next.numpy(), step + 1
16
17        x = x_next
18
19    return x.numpy(), max_steps
20
21root, steps = halley_root(initial_guess=1.5)
22print(root, steps)
23print(quartic(tf.constant(root, dtype=tf.float64)).numpy())

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 float32 when 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 float64 and 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.

Course illustration
Course illustration

All Rights Reserved.