maths.numerical_analysis.brent_method

Functions

brent_method(→ float)

Find the root of function func in the interval [left, right] using Brent's Method.

Module Contents

maths.numerical_analysis.brent_method.brent_method(func: collections.abc.Callable[[float], float], left: float, right: float, tol: float = 1e-08, max_iter: int = 100) float

Find the root of function func in the interval [left, right] using Brent’s Method.

Brent’s Method combines bisection, secant, and inverse quadratic interpolation.

Parameters

funcCallable[[float], float]

Function for which to find the root.

leftfloat

Left endpoint of interval.

rightfloat

Right endpoint of interval.

tolfloat

Tolerance for convergence (default 1e-8).

max_iterint

Maximum number of iterations (default 100).

Returns

float

Approximate root of func in [left, right].

Raises

ValueError

If func(left) and func(right) do not have opposite signs.

Examples

>>> def f(x): return x**3 - x - 2
>>> round(brent_method(f, 1, 2), 5)
1.52138
>>> def f2(x): return x**2 + 1
>>> brent_method(f2, 0, 1)
Traceback (most recent call last):
...
ValueError: func(left) and func(right) must have opposite signs