Skip to content
Tactiverge

Numerical root finding

Solve equations that have no neat formula: the bisection method, which always works if you can bracket a root, and Newton's method, which is far faster but can fail.

Lesson 3 of 6 · 35 min · Self-paced · Free

Why engineers solve equations numerically

Many engineering questions come down to "where does this quantity become zero?" At what speed does a drive motor's torque exactly balance the load, so the robot stops accelerating? At what angle does an arm's tip reach a target height? Where does a beam's deflection equal its limit? Rewrite each as f(x) = 0, and the answer is a root of f.

Some equations can be solved with algebra. Many cannot, or the algebra is messy. Numerical root finding gives an answer to any accuracy you choose by improving a guess step by step. Methods like these also run inside calculators, spreadsheets and simulation software whenever they solve an equation.

The idea behind bisection

Suppose f is a smooth, unbroken curve, f(a) is negative and f(b) is positive. Then somewhere between a and b the curve must cross zero. (This is the intermediate value theorem, but the picture is enough: you cannot walk from below sea level to above it without crossing the shoreline.) The interval [a, b] brackets a root.

Now look at the midpoint m. Either f(m) has the same sign as f(a), so the root lies between m and b, or it does not, so the root lies between a and m. Either way we keep a bracket half as wide. Repeat until the bracket is narrower than the tolerance you need.

def bisection(f, a, b, tol):
    """Find a root of f between a and b to within tol."""
    if f(a) * f(b) > 0:
        raise ValueError("f(a) and f(b) must have opposite signs")
    steps = 0
    while b - a > tol:
        mid = (a + b) / 2
        if f(a) * f(mid) <= 0:   # sign change in the left half
            b = mid
        else:                    # sign change in the right half
            a = mid
        steps += 1
    return (a + b) / 2, steps

def f(x):
    return x * x - 2           # its positive root is √2

print(bisection(f, 1.0, 2.0, 1e-6))

Output:

(1.4142136573791504, 20)

The true value is √2 = 1.41421356…, and the answer is within the tolerance of one millionth. Notice that bisection takes the function f itself as a parameter. In Python, functions are values that can be passed around like numbers, so one root finder works for any equation.

How many steps?

Each step halves the bracket. Starting with width 1, after n steps the width is 1/2ⁿ. We need 1/2ⁿ < 10⁻⁶, so 2ⁿ > 1,000,000, which first happens at n = 20. Bisection's cost is completely predictable — another O(log) algorithm, like binary search, and for exactly the same reason.

An engineering example: when does the motor stop accelerating?

A small motor's torque falls roughly linearly with speed. Suppose (made-up but realistic numbers) it gives 0.5 N·m at standstill and zero at 30 rad/s. The load it drives needs 0.05 N·m to overcome friction, plus a term that grows with the square of speed, 0.0004 ω². The robot speeds up while the motor torque exceeds the load torque and settles where they are equal, so we want the root of the net torque:

def bisection(f, a, b, tol):
    if f(a) * f(b) > 0:
        raise ValueError("f(a) and f(b) must have opposite signs")
    steps = 0
    while b - a > tol:
        mid = (a + b) / 2
        if f(a) * f(mid) <= 0:
            b = mid
        else:
            a = mid
        steps += 1
    return (a + b) / 2, steps

def net_torque(w):
    motor = 0.5 * (1 - w / 30)       # N·m, falls to zero at 30 rad/s
    load = 0.05 + 0.0004 * w * w     # N·m, friction plus a drag-like term
    return motor - load

w, steps = bisection(net_torque, 0.0, 30.0, 1e-6)
print(f"{w:.4f} rad/s after {steps} steps, residual {net_torque(w):.1e} N·m")

Output:

18.6512 rad/s after 25 steps, residual 4.1e-09 N·m

The bracket [0, 30] is a natural choice: at standstill the net torque is +0.45 N·m, and at 30 rad/s the motor gives nothing, so it is negative. The residual, f evaluated at the answer, is tiny — always check it, because it confirms you really found a root. This particular equation is a quadratic, so we can verify with the quadratic formula: the positive root is 18.6512 rad/s. Testing a numerical method on a problem with a known exact answer is how you build trust in it before using it on problems you cannot solve by hand.

Newton's method

Bisection only uses the sign of f. Newton's method uses its slope too. At the current guess x, draw the tangent line to the curve and follow it down to where it crosses zero. That crossing is the next guess:

x_next = x − f(x) / f′(x)

where f′(x) is the derivative, the slope of f at x. For f(x) = x² − 2, the slope is f′(x) = 2x:

def newton(f, dfdx, x, tol=1e-12, max_steps=50):
    for step in range(1, max_steps + 1):
        x_new = x - f(x) / dfdx(x)
        print(f"step {step}: x = {x_new:.15f}")
        if abs(x_new - x) < tol:
            return x_new
        x = x_new
    raise RuntimeError("Newton's method did not converge")

def f(x):
    return x * x - 2

def dfdx(x):
    return 2 * x

newton(f, dfdx, 1.0)

Output:

step 1: x = 1.500000000000000
step 2: x = 1.416666666666667
step 3: x = 1.414215686274510
step 4: x = 1.414213562374690
step 5: x = 1.414213562373095
step 6: x = 1.414213562373095

Look at how the correct digits grow: about 1, then 3, then 6, then 12, then full precision. Near a root, Newton's method roughly doubles the number of correct digits at each step. Five steps reach full double precision; bisection, gaining one binary digit per step, would need about 50.

When Newton's method fails

That speed has a price. Newton's method has no bracket, so nothing guarantees it stays near the root:

  • If the slope is zero, the formula divides by zero. If it is merely small, the next guess flies far away.
  • It can cycle. For f(x) = x³ − 2x + 2 starting at x = 0, the steps go 0, 1, 0, 1, … forever.
  • If an equation has several roots, it may find a different one from the one you wanted.
  • It needs the derivative, which you may have to work out (or estimate numerically).
def f(x):
    return x**3 - 2 * x + 2

def dfdx(x):
    return 3 * x**2 - 2

x = 0.0
for step in range(1, 5):
    x = x - f(x) / dfdx(x)
    print(f"step {step}: x = {x}")

Output:

step 1: x = 1.0
step 2: x = 0.0
step 3: x = 1.0
step 4: x = 0.0

That is why newton above has a max_steps limit and raises an error instead of looping forever. Professional solvers often combine the two ideas: they keep a bracket for safety and take Newton-like steps for speed whenever those steps stay inside it.

Choosing a method

  • Bisection: needs a bracket with a sign change; slow but guaranteed, with a predictable number of steps.
  • Newton: needs a good starting guess and the derivative; extremely fast when it works, but can diverge or cycle.
  • Either way: set a tolerance and a step limit, check the residual, and test on a problem with a known answer.

Recap

  • A root is where f(x) = 0; many engineering questions can be written this way.
  • Bisection halves a bracketing interval each step; n steps shrink it by 2ⁿ.
  • Newton's method follows tangent lines and roughly doubles correct digits per step near the root.
  • Newton's method can fail by dividing by a tiny slope, cycling or finding the wrong root.
  • Always limit steps, check residuals and verify against a known case.

Knowledge check

Check your understanding

  1. 1. Bisection on an interval of width 8 runs for 3 steps. How wide is the bracket now?
  2. 2. Which condition must hold before bisection can start on [a, b]?
  3. 3. Newton's method uses which formula for the next guess?
  4. 4. Newton's method starting at x = 0 alternates 0, 1, 0, 1, … for one equation. What is the best response?
  5. 5. Why compute the residual f(x) at the answer?

Project

Programming project: a root-finding race

Compare bisection and Newton's method on paper and in Python.

  1. By hand, with a calculator, do four steps of bisection for x² − 5 = 0 on [2, 3], and three steps of Newton's method starting at x = 2. Record each guess.
  2. In Python, solve the same equation with both methods to a tolerance of 10⁻¹⁰. Print the guesses at each step and the number of steps.
  3. Solve cos(x) = x (rewrite it as f(x) = cos(x) − x). Find a bracket by tabulating f at x = 0, 0.25, 0.5, … , 1.5 first.
  4. Try Newton's method on x³ − 2x + 2 = 0 from x = 0 and from x = −2. Describe what happens, and use bisection to find the real root reliably.

No hardware is required.

Deliverables

  • Hand-worked tables of guesses for both methods
  • A Python 3 program with both root finders and printed step-by-step output
  • A short paragraph comparing speed and reliability, using your results

Go further