Finding x where f(x) = 0 is equivalent to minimizing |f(x)|. optimize.root_scalar and optimize.fsolve solve scalar and multivariate equations.
Scalar roots
root_scalar— bracket or start point, methods like Brent- Requires sign change in bracket for many methods
- Example: solve x² − 2 = 0 for √2
Multivariate fsolve
fsolve(func, x0) solves F(x) = 0 vector equations—nonlinear circuit analysis, equilibrium models.
Example
import numpy as np
from scipy import optimize
def f(x):
return x ** 2 - 2
sol = optimize.root_scalar(f, bracket=[0, 2], method='brentq')
print('root:', sol.root)
Important interview questions and answers
- Q: Root vs minimum?
A: Root finding targets zero crossing; optimization targets lowest f—related but different APIs. - Q: Bracket requirement?
A: Brent needs f(a) and f(b) with opposite signs—continuous function between.
Self-check
- When use root_scalar vs fsolve?
- What does brentq require about the bracket?
Tip: For Brent methods, confirm f(a)*f(b) < 0 on your bracket.
Interview prep
- root_scalar?
Scalar f(x)=0 with bracket—Brent-style methods.
- fsolve?
Multivariate F(x)=0—nonlinear systems.