Web Simulation 

 

 

 

 

Root Finding: Bisection, Newton-Raphson, and Secant 

This interactive tutorial compares three root-finding algorithms: Bisection (robust but slow), Newton-Raphson (fast, uses derivative), and Secant (no derivative, superlinear convergence). Enter a function f(x) in JavaScript (e.g. x*x - 2 or Math.sin(x)), or choose Custom (Draggable Spline) to design a curve by dragging control points. Select a method from the dropdown and step through iterations. For Bisection the interval [a, b] is highlighted; for Newton the tangent from (xn, f(xn)) to the x-axis (yellow dot = current, red dot = next); for Secant the line through (xn−1, f(xn−1)) and (xn, f(xn)) to the x-axis (gray = previous, yellow = current, red = next).

Newton uses a numerical derivative (symmetric difference quotient) so you do not type the derivative. Use Step Fwd to advance one iteration, Step Bwd to undo, and Reset to restart. Click on the canvas: in Newton mode the click sets x0; in Bisection or Secant mode the first click sets a, the second sets b (Secant uses them as xn−1 and xn). The table shows Iteration, xn, and f(xn). Warnings appear for horizontal tangent (Newton), zero slope (Secant), or IVT violation (Bisection).

NOTE: Use Preset for challenge functions (Quadratic, Sine, Newton's Trap, Divergence Test, Logarithmic) or Custom to build a spline by dragging points.

 

Math behind the simulation

1. Goal

Find a root x* such that f(x*) = 0. We assume f is continuous (Bisection) and, for Newton-Raphson, differentiable.

2. Bisection method

Requires a bracket [a, b] with f(a) · f(b) < 0 (opposite signs), so by the Intermediate Value Theorem there is at least one root in (a, b).

  • Compute c = (a + b) / 2 and f(c).
  • If f(a) · f(c) < 0, the root lies in [a, c]: set b = c. Otherwise set a = c.
  • Repeat until |f(c)| or |b − a| is below tolerance.

Convergence is linear (error halves each step) but guaranteed as long as the bracket is valid.

3. Newton-Raphson method

Start from an initial guess x0. Approximate f by its tangent at xn and set the next iterate to the x-intercept of that tangent:

xn+1 = xn − f(xn) / f '(xn).

Convergence can be quadratic near a simple root but fails if f '(xn) ≈ 0 (horizontal tangent) or if the guess sends the sequence away from the root.

4. Secant method

Uses two previous iterates xn−1 and xn instead of the derivative. Draw the secant line through (xn−1, f(xn−1)) and (xn, f(xn)); the next iterate xn+1 is the x-intercept of this line:

xn+1 = xn − f(xn) · (xn − xn−1) / (f(xn) − f(xn−1)).

The slope of the secant (f(xn) − f(xn−1)) / (xn − xn−1) approximates f '(xn), so the Secant method is a derivative-free variant of Newton. Convergence is superlinear (order φ ≈ 1.618, the golden ratio). The method fails if f(xn) = f(xn−1) (zero slope, division by zero).

5. Numerical derivative

For Newton only, the simulator uses the symmetric difference quotient:

f '(x) ≈ [ f(x + h) − f(x − h) ] / (2h), with a small h (e.g. 10−6 · (1 + |x|)).

So you only type f(x); the derivative is approximated automatically. Secant needs no derivative.

 

Usage Example

Follow these steps to explore the Root Finding simulation:

  1. Load a preset: Use the Preset dropdown (Quadratic, Sine, Newton's Trap, Divergence Test, Logarithmic, or Custom (Draggable Spline)). The f(x) field updates except in Custom mode, where you drag control points on the canvas to shape the curve.
  2. Choose method: Use the Method dropdown: Newton (one initial point x0), Bisection (bracket [a, b] with opposite signs), or Secant (two starting points a, b as xn−1 and xn).
  3. Click-to-set: Click on the graph. In Newton mode, the click sets x0. In Bisection or Secant, the first click sets a, the second sets b. For Bisection, if f(a)·f(b) > 0 a warning appears; Secant has no sign requirement.
  4. Step Fwd / Step Bwd / Reset: Step Fwd advances one iteration; Step Bwd undoes the last step; Reset clears the table and reinitializes (keeps current sliders/click positions).
  5. Read the table: The table shows Iteration, xn, and f(xn). Newton and Secant converge quickly; Bisection halves the interval each step.
  6. Observe failures: Newton fails with horizontal tangent; Secant fails if f(xn) = f(xn−1). Use Custom to design curves that stress each method.

Tip: Start with Quadratic (x*x - 2) and try all three methods. Use Custom to drag points into a shape that causes Newton or Secant to diverge.

Parameters

  • Preset: Quadratic, Sine (multiple roots), Newton's Trap (local min/max), Divergence Test (arctan), Logarithmic (Bisection), or Custom (Draggable Spline) — drag points on the canvas to define the curve.
  • f(x): JavaScript expression in variable x (disabled in Custom mode). Newton uses a numerical derivative (symmetric difference quotient).
  • Method: Newton (one initial guess x0), Bisection (bracket [a, b] with f(a)·f(b) < 0), or Secant (two points a, b as xn−1, xn).
  • Tolerance: Convergence when |f(x)| < 10−8 (internal).

Controls and visualizations

  • Preset: Dropdown to load a challenge function; updates f(x) and resets the simulation.
  • f(x): Text input for the target function. Turns red if Bisection bracket has same sign (IVT violated).
  • Method toggle: Newton vs Bisection. Resets state when switched.
  • Step: Advance one iteration; stops Run if playing.
  • Run / Stop: Start or stop continuous stepping.
  • Reset: Clear iteration history and reinitialize; keeps last click position for Newton or current bracket for Bisection.
  • Canvas: Graph of f(x) with axes and grid. Bisection: green translucent overlay on [a, b] and vertical dashed line at midpoint. Newton: green dashed tangent from (xn, f(xn)) to x-axis, and dotted vertical line back to the curve; label shows xn+1.
  • Table: Iteration #, xn, f(xn) updated each step.
  • Toast: Red/warning messages for horizontal tangent (Newton), IVT violation (Bisection), or convergence.

Key concepts

  • Bisection: Guaranteed convergence if f(a)·f(b) < 0; error halves each step (linear). Requires a valid bracket.
  • Newton-Raphson: xn+1 = xn − f(xn)/f '(xn). Quadratic convergence near a simple root; fails if f ' ≈ 0 or guess is poor.
  • Secant: xn+1 = xn − f(xn)(xn−xn−1)/(f(xn)−f(xn−1)). No derivative; superlinear convergence (order φ). Fails if f(xn)=f(xn−1).
  • IVT: If f is continuous on [a, b] and f(a), f(b) have opposite signs, there is a root in (a, b). Bisection uses this.
  • Numerical derivative: Used only for Newton; f '(x) ≈ [f(x+h)−f(x−h)]/(2h).