optimize.minimize_scalar optimizes f(x) for a single real variable—ideal when you can bracket a minimum or search within bounds.
Methods
method='brent'— bracketed univariate (default)method='bounded'— requiresbounds=(a, b)method='golden'— golden-section search
Bracket intuition
Provide bracket=(a, b) where f(a) > f(c) < f(b) for some interior c—or use bounds. Wrong brackets miss the minimum.
Example
import numpy as np
from scipy import optimize
def f(x):
return (x - 1.5) ** 2 + 0.1 * np.sin(10 * x)
res = optimize.minimize_scalar(f, bracket=(0, 3))
print('x:', res.x, 'f(x):', res.fun)
Important interview questions and answers
- Q: When scalar vs vector minimize?
A: One decision variable (step size, learning rate grid) → scalar; multiple parameters → minimize. - Q: bounded method?
A: Use when x must stay in an interval—e.g. probability in [0, 1].
Self-check
- What does bracket mean for Brent's method?
- Write a scalar function with a known minimum at x=2.
Pitfall: Wrong bracket → wrong minimum—plot the scalar function first.
Interview prep
- When scalar?
Single decision variable—bracket or bounded search.
- bounded method?
Parameters constrained to interval—probabilities, physical limits.