bisection.bisection.bisection

bisection.bisection.bisection(f, xmin, xmax, tol=1e-09, max_iter=500)

Find a root of a scalar function using the bisection method.

Parameters

Name Type Description Default
f callable Function whose root is sought. Must accept a single scalar argument. required
xmin float Lower bound of the initial interval. required
xmax float Upper bound of the initial interval. required
tol float Absolute convergence tolerance. Convergence is achieved when \|xmax - xmin\| < tol. Default is 1e-9. 1e-09
max_iter int Maximum number of iterations allowed. Default is 500. 500

Returns

Name Type Description
root float Estimated root of the function f.

Raises

Name Type Description
ValueError If the initial interval [xmin, xmax] does not bracket a root (that is, if f(xmin) and f(xmax) have the same sign).
RuntimeError If the algorithm fails to converge within max_iter iterations.
TypeError If the inputs are not of the expected types.

Notes

The bisection method requires the root to be enclosed within the initial interval [xmin, xmax] such that:

:math:f(x_{\min}) \cdot f(x_{\max}) < 0

Convergence is guaranteed for continuous functions when this condition is satisfied and:

:math:|x_{\max} - x_{\min}| < \mathrm{tol}

Examples

>>> root = bisection(lambda x: 3*x**3 + 4*x**2 - 2*x - 2, 0, 2, max_iter=500)
>>> f"{root:.5f}"
'0.74827'