Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 ยท Optimization

Scalar Minimization and Root Finding

Find the minimum or the zero of a one-variable function with minimize_scalar, brentq, bisect, newton and root_scalar. Learn brackets and read convergence info.

Two questions come up constantly in numerical work. At which x is this function smallest? and at which x does this function equal zero? The first is minimization, the second is root finding. When the function takes a single number and returns a single number, SciPy gives you fast, robust tools for both, and they are the building blocks behind the multi-variable solvers in the lessons that follow.

Typical uses: the break-even price where profit crosses zero, the angle that maximizes a projectile's range, the interest rate that makes a stream of payments worth a given amount, or the temperature at which an equation of state balances. In each case you have a Python function f(x) that you can evaluate, but no neat formula for the answer.

All of these live in scipy.optimize. If you are new to the library, What Is SciPy? gives the map of submodules.

Minimizing a function of one variable

minimize_scalar finds a local minimum of f(x). To find a maximum, minimize -f(x). Its default method, Brent's, combines a safe interval-shrinking search with fast parabolic steps. It returns an OptimizeResult object with the fields you will meet again in every optimizer: x (the argmin), fun (the minimal value), success, message, and counters nit and nfev (iterations and function evaluations).

Python minimize_scalar basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the function has a ripple from the sine term. The unconstrained search lands in the valley near x = 2.16. When we restrict the search to [0, 1.5] with the bounded method, it settles in a different, shallower dip near x = 1.25. minimize_scalar finds a local minimum, never promises the global one. Three methods are available:

Method Needs Notes
brent (default) an optional bracket Fast and robust for smooth functions.
golden an optional bracket Slower, but needs no smoothness at all.
bounded bounds=(lo, hi) Restricts the search to an interval; use it when the function is only valid there.

What is a bracket?

A bracket for a minimum is a triple (a, b, c) with a < b < c, where f(b) is lower than both f(a) and f(c). A smooth function must have a minimum somewhere between a and c. If you pass only two numbers, they are starting points from which SciPy searches downhill until it builds a valid triple.

Root finding: where is f(x) = 0?

A root of f is a number x with f(x) = 0. Any equation g(x) = h(x) becomes root finding by moving everything to one side: f(x) = g(x) - h(x).

The most reliable approach uses a sign-change bracket [a, b]: an interval where f(a) and f(b) have opposite signs. If f is continuous, the intermediate value theorem guarantees a root inside. The bracketing solvers shrink the interval around it and cannot lose it.

flowchart TD
    S["f(a) and f(b)<br/>have opposite signs?"] -->|yes| B["brentq or bisect<br/>guaranteed to converge"]
    S -->|no| N["Do you know a good guess?"]
    N -->|yes| D["newton<br/>(add fprime if you can)"]
    N -->|no| F["Plot f, scan for a sign change,<br/>then use a bracket"]

A practical decision path: prefer a bracket, use Newton when you only have a guess.

Python brentq, bisect and newton

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All four methods agree on the root near 2.0945514815. Here is how they differ:

  • bisect halves the interval each step. It is slow (about one bit of accuracy per call) but bulletproof.
  • brentq is Brent's method. It mixes bisection with faster interpolation steps and never does worse than bisection by much. It is the standard recommendation when you have a bracket.
  • newton needs only a starting point. With fprime it is the classic Newton-Raphson step x - f(x)/f'(x), which converges extremely fast near a simple root. Without fprime it falls back to the secant method (a numerical slope from two points). It has no safety net, so it can fail.

One interface for all: root_scalar

root_scalar wraps these methods behind a single call and returns a result object with everything you need to judge convergence: root, converged, iterations and function_calls. That makes comparisons easy. Let us solve cos(x) = x with four methods and tight tolerance:

Python Comparing root-finding methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the table of results as a lesson in cost. Bisection needs about forty iterations to reach 1e-12. Brent and the secant method need around seven. Newton needs the fewest iterations but also counts the derivative calls in function_calls, so its total evaluations are in the same range as the others. When your function is expensive (a simulation, a database query), function_calls is the number to watch.

Here is the root drawn on the curve, with a plot that is useful as a sanity check before trusting any solver:

Python Plot the function and its root

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Tolerances and convergence diagnostics

Every solver stops when a tolerance is met: xtol (how small the interval or step is, an absolute error in x), rtol (a relative error), and maxiter. Defaults are tight, around 2e-12 for xtol, which is far more than most real problems need. Loosening them saves calls when f is expensive.

Do not trust a result blindly. Check:

  1. converged (or success) is True.
  2. abs(f(root)) is small. A tiny step does not always mean a tiny residual, especially for steep functions.
  3. The answer makes sense physically or in your plot.

By default brentq and newton raise an error when something is wrong, which is good: silent failure is worse. Pass disp=False to newton or inspect full_output to handle it yourself.

Common failure modes

Python When things go wrong

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What this shows:

  • A bracket must straddle a sign change. x**2 + 1 is always positive, so it has no real roots and brentq refuses.
  • Newton's method is fast but fragile. On this function a start of 0 bounces between 0 and 1 forever. The bracketed brentq has no such problem.
  • A bracket isolates one root. For several roots, scan the function on a grid for sign changes and call brentq on each interval.
  • The function xโด - 3xยฒ + x has two valleys. The bracket you provide decides which one you get; the left one is deeper (the global minimum), the right one is only local. Plot first, or run from several brackets and compare fun.

A root that touches zero without crossing, like (x - 1)**2, has no sign change, so bracketing solvers cannot find it. Minimize f(x)**2 instead, or work with the derivative.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must be true of the interval [a, b] you give to brentq?

    A sign change guarantees (for a continuous function) that a root lies inside. brentq raises ValueError if f(a) and f(b) have the same sign.
  2. 2. How do you find the MAXIMUM of f(x) with minimize_scalar?

    SciPy only minimizes. The maximum of f is the minimum of -f, and the maximal value of f is the negative of the minimal value found.
  3. 3. Which method uses a derivative to converge very fast but can diverge or cycle?

    Newton-Raphson uses f'(x) (or a secant estimate) and converges quickly near a simple root, but it has no bracket to keep it safe and can cycle or jump away.
  4. 4. minimize_scalar returns a result with success=True. What does this guarantee?

    success only says the algorithm stopped normally. The answer is a local minimum near the bracket; compare brackets or plot the function to look for better valleys.

Summary

  • minimize_scalar finds a local minimum of a one-variable function; methods are brent, golden and bounded. Maximize by minimizing -f.
  • A minimum bracket is (a, b, c) with f(b) below both ends; a root bracket is an interval with a sign change.
  • brentq is the first choice when you have a root bracket; bisect is the slow, certain fallback; newton is fast but needs a good starting guess.
  • root_scalar unifies the methods and returns root, converged, iterations and function_calls.
  • Always check convergence flags and abs(f(root)), and plot the function when in doubt.
  • Roots that only touch zero, several roots, and multiple valleys all need extra care.

Next up: Minimizing Functions of Many Variables, where x becomes a vector.