Bracketed equation solving
Solve a scalar nonlinear equation when a valid interval containing a root is known.
SciPy · Function
Finds a zero of a continuous function inside a sign-changing interval using Brent’s method. It combines bracketing, bisection, and inverse quadratic interpolation.
brentq searches for a zero of a continuous function between two endpoints whose function values have opposite signs. It combines bracketing and bisection with inverse quadratic interpolation, stopping when the requested absolute and relative tolerances are met or the iteration limit is reached.
scipy.optimize.brentq(f, a, b, args=(), xtol=2e-12, rtol=np.float64(8.881784197001252e-16), maxiter=100, full_output=False, disp=True)| Name | Type | Description |
|---|---|---|
f | callable | A callable whose zero is sought. |
a | scalar | One scalar endpoint of the bracketing interval [a, b]. |
b | scalar | The other scalar endpoint of the bracketing interval [a, b]. |
args | tuple, optional | Extra positional arguments passed to f; default (). |
xtol | number, optional | Absolute tolerance for the computed root; default 2e-12. |
rtol | number, optional | Relative tolerance; default np.float64(8.881784197001252e-16). |
maxiter | int, optional | Maximum iterations; default 100. |
full_output | bool, optional | If true, return root information in addition to the root; default false. |
disp | bool, optional | If true, raise on failure to converge; default true. |
If full_output is False, the root as a float. If full_output is True, a tuple containing the root and a RootResults object.
from scipy.optimize import brentq
root = brentq(lambda x: x**2 - 2, 0, 2)
print(root)Finds the square-root of 2 by solving x² − 2 = 0 on an interval whose endpoint values have opposite signs.
Solve a scalar nonlinear equation when a valid interval containing a root is known.
Compute a threshold or crossing point of a continuous model over a bounded range.
Obtain a robust one-dimensional root without requiring a derivative.
brentq finds a zero of f inside the interval [a, b] using Brent’s method. The method combines root bracketing, interval bisection, and inverse quadratic interpolation.
Follow-up: Why are endpoint signs required? They establish a bracket for a continuous function, so the interval contains a zero under the intermediate value theorem.
f is the callable, while a and b are the scalar interval endpoints. Additional positional values can be supplied through args, which are passed to f.
Follow-up: How would you pass two parameters? Define f(x, p, q) and call brentq(f, a, b, args=(p, q)).
By default, the function returns the root as a float. With full_output=True it returns the root together with a RootResults object; disp controls whether failure raises an exception.
Follow-up: What happens if the iteration limit is reached while disp is false? The result object can report convergence status when full_output is enabled.
scipy.optimize.brenthscipy.optimize.bisectscipy.optimize.newtonscipy.optimize.root_scalar