cmlabs.optimize.bisect

cmlabs.optimize.bisect(f, bracket, xtol=None, ytol=None)[source]

Bisection method for finding roots.

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

  • bracket (A sequence of 2 floats) – THe root interval. The function must have different signs at the endpoints.

  • 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

The function passed to this method must satisfy The Bolzano–Cauchy theorem.

Parameter xtol is the absolute error in x required to declare convergence.

\[\begin{aligned} |x^* - x| < \text{xtol} \end{aligned}\]

where \(x^*\) is the root of the function.

Parameter ytol is the absolute error in f(x) required to declare convergence.

\[\begin{aligned} |f(x^*) - f(x)| < \text{ytol} \end{aligned}\]

where \(x^*\) is the root of the function.

You can define both xtol and ytol to declare convergence. The first one that is satisfied will be used to declare convergence.

Examples

>>> from cmlabs.optimize import bisect
>>> f = lambda x: x**2 - 4
>>> bracket = [0, 10]
>>> bisect(f, bracket)
>>> # 1.9999980926513672