""" Ex. A.10 from "A primer on..." Implement the secant method. """ import numpy as np import matplotlib.pyplot as plt def secant(f, x0, x1, tol=1e-6): n = 0 while abs(f(x1)) > tol: x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)) x0 = x1 #x_{n-1} => x_{n-2} x1 = x2 #x_n => x_{n-1} n += 1 return x2, n #solve x**5 - sin(x) = 0: def g(x): return x**5 - np.sin(x) x = secant(g, 1.5, 1.4) print(x)