Implement Newton's method for finding the minimum of a single-variable function. Given a function, its first derivative, and its second derivative, iteratively update the estimate until convergence.
def newtons_method(f, df, ddf, x0: float, tol: float = 1e-6, max_iter: int = 100) -> float:
x = x0
for _ in range(max_iter):
first_deriv = df(x)
second_deriv = ddf(x)
if abs(second_deriv) < 1e-12:
break
x_new = x - first_deriv / second_deriv
if abs(x_new - x) < tol:
return round(x_new, 6)
x = x_new
return round(x, 6)x0.x_new = x - f'(x) / f''(x).