Root Finding Methods
Find $x$ such that $f(x) = 0$.
Newton's Method
$$ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} $$
1def newton(f, df, x0, tol=1e-6, max_iter=100):
2 """Newton's method for root finding"""
3 x = x0
4 for i in range(max_iter):
5 fx = f(x)
6 if abs(fx) < tol:
7 return x
8 x = x - fx / df(x)
9 return x
10
11# Example: find sqrt(2)
12f = lambda x: x**2 - 2
13df = lambda x: 2*x
14root = newton(f, df, x0=1.0)
15print(f"√2 ≈ {root:.10f}")
Bisection Method
1def bisection(f, a, b, tol=1e-6):
2 """Bisection method (slower but guaranteed)"""
3 while (b - a) / 2 > tol:
4 c = (a + b) / 2
5 if f(c) == 0:
6 return c
7 elif f(a) * f(c) < 0:
8 b = c
9 else:
10 a = c
11 return (a + b) / 2
SciPy
1from scipy.optimize import fsolve, brentq
2
3# Newton-type
4root = fsolve(f, x0=1.0)[0]
5
6# Brent's method (robust)
7root = brentq(f, a=0, b=2)
Further Reading
Related Snippets
- Interpolation Methods
Linear, polynomial, and spline interpolation - Numerical Differentiation
Finite differences and automatic differentiation - Numerical Integration
Trapezoidal rule, Simpson's rule, Gaussian quadrature - Optimization Methods
Gradient descent, Newton's method, BFGS - Regularization Techniques
L1, L2, Tikhonov, and elastic net regularization - Solving Linear Systems
LU decomposition and iterative methods