Chris Collins

Tools · math

Root finding, step by step

Bisection, Newton, secant and fixed-point iteration side by side, one step per click, with tangents, secants, a cobweb diagram, an iterate table and a log error plot; Newton's failures (flat tangents, cycles, divergence), basins of attraction, the complex z³ − 1 fractal, and Babylonian square roots digit by digit.

Added

How to use it

Type a function f(x) — x^2 - 2, cos(x) - x, x^3 - 2x + 2, atan(x) — and watch four ways of solving f(x) = 0 race each other. Everything runs in double precision, exactly as the book’s Python does, so the numbers agree digit for digit.

⛶ Full screen (or F) gives the open tab’s picture the whole screen — the four method plots, the Newton curve (with its basin strip above it), the complex-basin fractal or the Babylonian square — using the browser’s full screen where it is allowed, otherwise a full-window layer (iPhone). Everything is redrawn at the new size; Escape leaves.

Side by side

Set a bracket [a, b] (where f changes sign), a starting value x₀ and, for fixed-point iteration, a rearrangement x = g(x). Step ▶ takes one step with every method:

  • Bisection shades the bracket and marks the midpoint with the sign of f(m).

  • Newton follows the tangent at xₙ down to the axis. Type f′(x) for exact tangents; leave it blank and a numerical derivative is used.

  • Secant starts from a and b and follows the line through the last two points.

  • Fixed point draws the cobweb: up to y = g(x), across to y = x, and again.

The error plot shows |xₙ − root| on a log scale (bisection’s straight line, Newton’s plunge, the secant’s golden-ratio order 1.618, fixed point’s constant ratio |g′(r)|), and reports the first step at which each method gets below the tolerance (10⁻¹² unless the link sets tol). The table lists every iterate; pick one method to see its full table — a, b, m and f(m) for bisection; xₙ, f(xₙ), f′(xₙ) and the correction for Newton; the error ratio for fixed point.

Newton explorer

Drag x₀ (or click the curve) and see where Newton’s method goes, with each tangent drawn and a table of xₙ, f(xₙ), f′(xₙ) and the correction. The verdict says whether it converges, cycles, diverges, stops at a horizontal tangent, or wanders. The colour strip under the graph colours every starting value in view by the root it reaches — zoom in on x³ − x near ±0.447 to see the bands interleave. Animate replays the tangents one at a time; race bisection adds bisection on [a, b] to the error plot. For arctan x the page prints the exact divergence threshold, x₀ ≈ 1.3917452, where Newton bounces between x₀ and −x₀ for ever.

Complex basins

Newton’s method works unchanged on complex numbers. Every pixel is a starting value z₀, coloured by the root of z³ − 1 it reaches (darker: more steps). Click to zoom in ×2. Try z³ − 2z + 2 to see black islands of starting points trapped in the same 0 ↔ 1 cycle as on the real line.

Square roots

The Babylonian (Heron’s) method: average your guess g with a/g. Drag the side of a square until its area matches a, then step: g and a/g close in on √a from opposite sides, and the table shows the correct digits of each guess in green — 1, 3, 6, 12, then all 16 a double can hold.

Limits

  • Everything is IEEE double precision (about 16 significant digits); errors are measured against roots found to the last bit, so they bottom out near 10⁻¹⁶ and are drawn at 10⁻¹⁷ when exactly zero.

  • The numerical derivative (when f′ is blank) is a 5-point central difference, accurate to about 12 digits for smooth f; Newton then still converges, but the last digits are not quadratic.

  • Roots for the error plots are found by scanning [−20, 20] (wider when your inputs are), so a root far outside that range may be missed.

  • Steps are capped at 60 (side by side) and 100 (explorer).

Related: Graphing calculator (roots, extrema, intersections), Where floating point bites (cancellation, the V-shaped error of numerical derivatives), Algebra step by step (the quadratic formula, factoring), Symbolic algebra (exact derivatives to paste into f′).

Companion to Mathematics, Reconnected: Powers and roots (the root finder), Quadratics (bisection versus Newton), Limits (the bisection stepper, Heron’s method), Applications of derivatives (Newton’s method explorer) and Numerical methods (root finding compared, Newton’s basins).

Linking to an example

Every setting can be written by hand in the address, so a link can open an exact state, for example /tools/root-finding/?f=x^3-2x%2B2&x0=0&n=6#newton. The part after # picks the tab: #steps, #newton, #fractal or #sqrt. Settings a link leaves out take their defaults.

Write ` in an expression as `%2B` to be safe. A bare ` also works: a URL turns it into a space, and the tool reads a space between two terms as plus (x^2+1 → x^2 1 → x^2+1). Write products without spaces (2x, 2*x).

Parameter Meaning Example

f

the function whose root is wanted

x^3-x-2

df

its derivative for Newton (blank: numerical)

3x^2-1

g

fixed-point rearrangement x = g(x)

cos(x)

a, b

bisection bracket; the secant method starts from a and b (numbers or expressions)

0, pi/2

x0

Newton’s and fixed-point iteration’s start

1.5

n

steps already taken (0–60)

7

method

side-by-side focus: all, bisect, newton, secant, fixed

newton

tol

tolerance for the "first step below" race

1e-12

xmin, xmax

graph view (blank: automatic)

-2.5, 2

race

add bisection on [a, b] to the Newton explorer’s error plot

true

poly

complex basins: z3-1, z3-2z+2, z3-z

z3-1

cx, cy, w

complex basins: view centre and width

0, 0, 4

it

complex basins: maximum Newton steps per pixel

40

sq

square roots: the number a in √a

50

g0

square roots: the first guess

7

hn

square roots: Babylonian steps taken

2

example

a book example (below); its settings win over any given beside it

ch18-newton-cycle

Book examples, as ?example=<name> (the tab is chosen for you):

Name Tab Shows

ch03-root-finder

sqrt

Root finder: √20 by the Babylonian method

ch03-sqrt50

sqrt

Exercise: two Babylonian steps for √50 from 7

ch08-bisection

steps

Bisection on x² − 2x − 1 over [2, 3]: 40 halvings

ch08-bisect-newton

steps

Bisection versus Newton on x² − 2

ch08-vertex

newton

Newton started exactly at the vertex: horizontal tangent

ch08-no-real-root

newton

Newton on x² + 1 wanders chaotically

ch16-bisection

steps

Bisection on x³ − x − 2 over [1, 2] (the hand table)

ch16-bisection-stepper

steps

Bisection stepper

ch16-asymptote

steps

Bisection on 1/x over [−1, 2] "converges" to the asymptote

ch16-cos-x

steps

cos x = x on [0, π/2]: three bisection steps

ch16-heron

sqrt

Heron’s method for √2 from 1

ch18-newton-sqrt2

steps

Newton (from 1) versus bisection (on [1, 2]) for √2

ch18-newton-cubic

newton

Newton’s own cubic x³ − 2x − 5 from 2

ch18-newton-cycle

newton

x³ − 2x + 2 from 0 cycles 0, 1, 0, 1, …

ch18-arctan

newton

arctan x from 1.5 diverges; the threshold is near 1.39

ch18-cbrt

newton

∛x: every Newton step doubles the distance

ch18-flat-tangent

newton

x² − 2 from 0.01: a near-flat tangent throws x₁ to 100

ch18-basins

newton

x³ − x: interleaved basins near ±0.447

ch18-basins-zoom

newton

x³ − x: zoom into the bands near 0.447

ch18-double-root

steps

(x − 1)²: a double root makes Newton merely linear

ch18-newton-explorer

newton

Newton’s method explorer

ch18-newton-fractal

fractal

Newton basins for z³ − 1 in the complex plane

ch32-compare

steps

Bisection, Newton, secant and fixed point on x² − 2

ch32-secant

steps

The secant method from 1 and 2

ch32-fixed-point

steps

Fixed point x ← x − (x² − 2)/3: ratio 0.057

ch32-fixed-2-over-x

steps

Fixed point x ← 2/x oscillates 1, 2, 1, 2

ch32-newton-basins

newton

Newton’s basins: x³ − 2x + 2

ch32-fractal

fractal

Fractal basins for z³ − 1

ch32-fractal-cycle

fractal

z³ − 2z + 2: black islands that never converge

Runs entirely in your browser; nothing you type leaves this page.