3 min read•august 1, 2024
The is a powerful tool for solving nonlinear equations without needing derivatives. It's like 's cool cousin, using two points to estimate the slope instead of calculus. This approach makes it super handy for tricky functions.
While it's not as fast as Newton's Method near the solution, the Secant Method shines when derivatives are a pain to calculate. It's a great balance of speed and simplicity, making it a go-to choice for many real-world problems.
def secant_method(f, x0, x1, tol=1e-6, max_iter=100): for i in range(max_iter): fx0, fx1 = f(x0), f(x1) if abs(fx1) < tol: return x1 if fx0 == fx1: raise ValueError("Division by zero encountered") x_new = x1 - fx1 * (x1 - x0) / (fx1 - fx0) x0, x1 = x1, x_new raise ValueError("Method did not converge")