5.4.25 · D3Scientific Computing (Python)

Worked examples — Implementing root-finding from scratch — Newton-Raphson, bisection

3,422 words16 min readBack to topic

This page is self-contained: everything it needs — the guard tests, the tangent formula, the error argument — is restated here in full before it is used. When a word is new, we rebuild it from zero.


The scenario matrix

Root-finding has a small number of situations that behave differently. Here is the full list. Each row's "Example" column names the section that works it (the labels C1…C10 match the headings below exactly).

# Case class What is special Which method Example
C1 Clean bracket, opposite signs textbook bisection bisection C1
C2 Sign convention: which half survives quadrant/sign bookkeeping bisection C2
C3 Endpoint is exactly a root degenerate: bisection C3
C4 Newton, good start, quadratic bliss Newton C4
C5 Newton near a flat spot () huge/undefined step Newton C5
C6 Newton that diverges / cycles limiting bad behaviour Newton C6
C7 Double root ( together) convergence slows to linear Newton C7
C8 Real-world word problem units, modelling either C8
C9 Exam twist: no derivative given pick the robust engine bisection C9
C10 Invalid bracket (), and midpoint hits root error case + early exit bisection C10

We will meet all three, plus the pure error cases.


C1 — Clean bracket, textbook bisection


C2 — Sign bookkeeping: which half survives


C3 — Degenerate: an endpoint is already the root


C4 — Newton, good start, quadratic bliss


C5 — Newton near a flat spot ()


C6 — Newton that diverges / cycles (limiting disaster)


C7 — Double root: quadratic convergence breaks down


C8 — Real-world word problem (units matter)


C9 — Exam twist: no derivative supplied


C10 — Invalid bracket, and a midpoint that lands on the root


Recall Quick self-test across the matrix

Which cell breaks Newton's quadratic convergence even with a perfect start? ::: C7 — a double root, because demotes it to linear. Why does starting at (C5) send Newton far away? ::: The slope is near zero, so the tangent is nearly flat and its x-intercept is enormous — a giant step. On (C6), what does the Newton update reduce to? ::: , doubling the distance and flipping sign each step — pure divergence. Given no derivative (C9), name two safe options. ::: Bisection or the secant method — both need only values. What two guards handle the bisection edge cases in C10? ::: if fa*fb > 0: raise (no valid bracket) and if fm == 0: return m (midpoint is exactly a root). What does "one bit of precision" mean for bisection? ::: One halving of the uncertainty interval — a single yes/no answer about which half hides the root. For a root of multiplicity , which derivatives vanish there? ::: but .

See also the parent note, Fixed-point iteration for another view of iteration, scipy.optimize for production tooling, and Floating point arithmetic for why f(m)==0 almost never happens exactly.