cmlabs.optimize.secant

cmlabs.optimize.secant(f, x0, x1, xtol=None, ytol=None)[source]

Secant method for finding roots.

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

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

  • x1 (float) – The second 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)\) using two initial guesses. It is a generalization of the Newton-Raphson method and does not require the computation of the derivative.

\[\begin{aligned} \overline{x}_{n+1} = \overline{x}_n - \frac{f(\overline{x}_n) (\overline{x}_n - \overline{x}_{n-1})} {f(\overline{x}_n) - f(\overline{x}_{n-1})} \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 in the neighborhood of the root.

Examples

>>> from cmlabs.optimize import secant
>>> f = lambda x: x**2 - 4
>>> x0 = 0.0
>>> x1 = 10.0
>>> secant(f, x0, x1, xtol=1e-5, ytol=1e-5)
>>> # 2.000000000826115