Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

optimize.brentq

Finds a zero of a continuous function inside a sign-changing interval using Brent’s method. It combines bracketing, bisection, and inverse quadratic interpolation.

How it works

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.

Syntax

scipy.optimize.brentq(f, a, b, args=(), xtol=2e-12, rtol=np.float64(8.881784197001252e-16), maxiter=100, full_output=False, disp=True)

Parameters

NameTypeDescription
fcallableA callable whose zero is sought.
ascalarOne scalar endpoint of the bracketing interval [a, b].
bscalarThe other scalar endpoint of the bracketing interval [a, b].
argstuple, optionalExtra positional arguments passed to f; default ().
xtolnumber, optionalAbsolute tolerance for the computed root; default 2e-12.
rtolnumber, optionalRelative tolerance; default np.float64(8.881784197001252e-16).
maxiterint, optionalMaximum iterations; default 100.
full_outputbool, optionalIf true, return root information in addition to the root; default false.
dispbool, optionalIf true, raise on failure to converge; default true.

Returns

If full_output is False, the root as a float. If full_output is True, a tuple containing the root and a RootResults object.

Key points

  • Use brentq to locate a scalar zero within a supplied bracketing interval.
  • The algorithm blends guaranteed interval reduction with faster interpolation steps.
  • a supplies one scalar endpoint of the search interval.
  • b supplies the second scalar endpoint.

Common mistakes

  • The function must be continuous across the interval, and the endpoint values must bracket a root by having opposite signs.

Example: Solve a bracketed equation

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.

When to use it

Bracketed equation solving

Solve a scalar nonlinear equation when a valid interval containing a root is known.

Finding a crossing

Compute a threshold or crossing point of a continuous model over a bounded range.

Derivative-free root finding

Obtain a robust one-dimensional root without requiring a derivative.

Interview questions

QWhat does brentq compute, and how does it search?

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.

QWhat are the main inputs?

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)).

QHow do output and failure behavior change?

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.brenth
  • scipy.optimize.bisect
  • scipy.optimize.newton
  • scipy.optimize.root_scalar

Source: brentq — SciPy v1.18.0 Manual. Checked 2026-10-15.