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
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
Bisection ke liye bracket [a,b] ko kaun si condition satisfy karni chahiye?
f(a)⋅f(b)<0 (ek sign change), Intermediate Value Theorem ke zariye.
Bisection ko loop se pehle endpoints kyun check karne chahiye?
Agar f(a)=0 ya f(b)=0 exactly hai, toh strict bracket test f(a)f(b)<0 fail hoga aur loop ek real root discard kar dega; endpoint ko immediately return karo.
Newton-Raphson update formula kya hai?
xn+1=xn−f′(xn)f(xn).
Newton formula geometrically kahan se aata hai?
xn par f 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 ∝en2, digits double hote hain).
Tolerance ε paane ke liye [a,b] par kitne bisection steps chahiye?
n≥log2εb−a−1.
Do tarike batao jisme Newton fail ho sakta hai.
f′(x)≈0 (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 x2−2 par kya ban jaata hai?
Babylonian method xn+1=2xn+xn1 for 2.
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 ∣xn+1−xn∣ (ya ∣f(x)∣) 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.