cmlabs.optimize.newton

cmlabs.optimize.newton(f, df, x0, xtol=None, ytol=None)[source]

Newton’s method for finding roots.

Parameters:
  • f (callable) – The function to find the root of.

  • df (callable) – The derivative of the function.

  • x0 (float) – The initial guess for the root.

  • xtol (float, optional) – The absolute error in x required to declare convergence.

  • ytol (float, optional) – The absolute error in f(x) required to declare convergence.

Return type:

float

Notes

This method is an iterative numerical technique for finding a root of a real-valued function \(f(x)\), based on linear approximation using the first derivative. The method requires the function to be sufficiently smooth and its derivative behavior to meet certain conditions to ensure convergence.

\[\begin{aligned} \overline{x}_{n+1} = \overline{x}_n - \frac{f(\overline{x}_n)}{f'(\overline{x}_n)} \end{aligned}\]

where \(\overline{x}_n\) is the current approximation of the root, and \(\overline{x}_{n+1}\) is the next approximation.

The function \(f(x)\) must be continuous and differentiable in the neighborhood of the root. The derivative \(f'(x)\) must not be zero at the root.

The starting point \(x_0\) should be chosen close to the actual root to ensure convergence. A practical sufficient condition for local convergence is \(f(x_0) \cdot f''(x_0) > 0\).

Examples

>>> from cmlabs.optimize import newton
>>> f = lambda x: x**2 - 4
>>> df = lambda x: 2 * x
>>> x0 = 5.0
>>> newton(f, df, x0, xtol=1e-5, ytol=1e-5)
>>> # 2.0000051812194735