5.4.25 · HinglishScientific Computing (Python)

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

1,925 words9 min readRead in English

5.4.25 · Coding › Scientific Computing (Python)


1. Bisection — squeeze a bracket

def bisection(f, a, b, tol=1e-10, maxit=200):
    fa, fb = f(a), f(b)
    if fa == 0:                  # Why? endpoint already exact root hai
        return a
    if fb == 0:
        return b
    if fa * fb > 0:
        raise ValueError("f(a) and f(b) must have opposite signs")
    for _ in range(maxit):
        m = (a + b) / 2          # Why? midpoint bracket ko evenly split karta hai
        fm = f(m)
        if fm == 0 or (b - a) / 2 < tol:
            return m              # Why? exact root, ya width tol se neeche -> kaafi achha
        if fa * fm < 0:          # Why? sign change LEFT half mein hai
            b, fb = m, fm
        else:                    # Why? sign change RIGHT half mein hai
            a, fa = m, fm
    return (a + b) / 2

2. Newton-Raphson — ride the tangent

def newton(f, df, x0, tol=1e-12, maxit=100):
    x = x0
    for _ in range(maxit):
        fx = f(x)
        dfx = df(x)
        if dfx == 0:
            raise ZeroDivisionError("zero derivative — tangent is flat")
        step = fx / dfx          # Why? -fx/dfx woh move hai tangent ke root tak
        x = x - step
        if abs(step) < tol:      # Why? step size batata hai kitna move kiya
            return x
    return x

Figure — Implementing root-finding from scratch — Newton-Raphson, bisection

3. Worked examples


4. Best of both: when to use which (80/20)

Bisection Newton
Chahiye bracket + achha
Convergence linear () quadratic ()
Guaranteed? yes (cont. f) no
Per-step cost ka 1 eval aur ka 1 eval

Flashcards

Bisection ke liye bracket ko kaun si condition satisfy karni chahiye?
(ek sign change), Intermediate Value Theorem ke zariye.
Bisection ko loop se pehle endpoints kyun check karne chahiye?
Agar ya exactly hai, toh strict bracket test fail hoga aur loop ek real root discard kar dega; endpoint ko immediately return karo.
Newton-Raphson update formula kya hai?
.
Newton formula geometrically kahan se aata hai?
par ki tangent line ke x-intercept se.
Bisection vs Newton ki convergence order?
Bisection linear hai (error har step mein half hoti hai); Newton quadratic hai (error , digits double hote hain).
Tolerance paane ke liye par kitne bisection steps chahiye?
.
Do tarike batao jisme Newton fail ho sakta hai.
(huge/undefined step) aur starting guess bahut door (divergence ya cycling).
Bisection "guaranteed" kyun hai lekin Newton nahi?
Bisection hamesha ek valid bracket maintain karta hai isliye root trapped rehta hai; Newton blindly ek tangent follow karta hai jo root se door point kar sakti hai.
Newton par kya ban jaata hai?
Babylonian method for .
scipy.optimize.brentq actually kaun sa algorithm use karta hai?
Brent's method = bisection combined with secant method aur inverse-quadratic interpolation (Newton's method NAHI — ise koi derivative nahi chahiye).
Newton ke liye ek achha stopping test kaun si quantity hai?
Step size (ya ) ek tolerance se neeche.

Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tumne apna khilona ek lambe hallway mein kahin kho diya aur tum jaante ho yeh rooms 1 aur 100 ke beech hai. Bisection = tum room 50 par jaate ho aur poochte ho "kya khilona yahan se pehle hai ya baad mein?" Phir tum sirf usi half mein search karte ho. Har sawaal hallway ko aadha kar deta hai — super safe, tum ise hamesha dhundhoge, bas kuch steps lagte hain. (Aur agar khilona bilkul room 1 ya 100 par hi baitha hai, toh pehle woh doors check karo!) Newton = tumhare paas ek magic arrow hai jo roughly khilone ki taraf point karta hai. Tum jahan point kare wahan sprint karte ho, naaya arrow dekho, phir sprint karo. Agar arrow achha hai toh tum 2 jumps mein pahunch jaate ho — lekin agar tum kisi weird spot se shuru karo toh arrow tumhe khidki se bahar fek sakta hai! Toh: arrow jab ho sake, halving jab zaroorat ho.

Connections

Concept Map

solved by

strategy A

strategy B

needs

justified by

halves interval

gives

follows

gives

slow but

tradeoff

edge case

Root where f x star equals 0

Iterate improve guess

Bisection

Newton-Raphson

Bracket f a times f b less than 0

Intermediate Value Theorem

Width equals b-a over 2 to n

Linear convergence ratio one half

Tangent line to x-intercept

Fast but fragile

Guaranteed robust

Check endpoints f a or f b equals 0