Nonlinear systems
Solving several nonlinear equations simultaneously
SciPy · Function
Find a zero of a vector-valued function from an initial guess. The function supports multiple solver methods, optional Jacobians, callbacks, and solver-specific options.
Call root with a callable residual function and an initial guess. SciPy selects the requested solver, optionally obtains or estimates the Jacobian, and returns an OptimizeResult containing the candidate root and termination information.
scipy.optimize.root(fun, x0, args=(), method='hybr', jac=None, tol=None, callback=None, options=None)| Name | Type | Description |
|---|---|---|
fun | callable | A vector function whose zero is sought. |
x0 | ndarray | Initial guess for the unknown vector. |
args | tuple, optional | Extra positional arguments passed to fun and its Jacobian. |
method | str, optional | Solver type; the default is hybr. |
jac | callable or bool, optional | Boolean or callable Jacobian specification. |
tol | float, optional | Termination tolerance. |
callback | function, optional | Optional iteration callback. |
options | dict, optional | Solver-specific options, such as xtol or maxiter. |
sol : OptimizeResult — the solution represented as an OptimizeResult object, including attributes such as x, success, and message.
import numpy as np
from scipy.optimize import root
def residual(x):
return np.array([x[0]**2 - 2.0])
sol = root(residual, x0=np.array([1.0]))
print(sol.x[0], sol.success)Defines a vector residual function, starts from an initial guess, and reads the solution and success flag from the returned OptimizeResult.
Solving several nonlinear equations simultaneously
Using analytical derivatives to improve solver behavior
Monitoring iterative progress with supported methods
root solves a vector equation by seeking an x for which fun(x) is zero. It accepts an initial guess x0 and returns an OptimizeResult rather than only the numerical root.
Follow-up: How would you provide extra data? Pass it through args; those values are forwarded to fun and, when applicable, its Jacobian.
The default method is hybr. Other documented choices include lm, the inexact-Newton family, and Krylov-based methods; the selected method determines the algorithm and available options.
Follow-up: Why might method selection matter? Solver-specific methods have different convergence behavior and option sets, so the problem structure should guide the choice.
jac may be a Boolean or a callable. If it is False, the Jacobian is estimated numerically; if it is callable, it must accept the same arguments as fun and return its Jacobian.
Follow-up: What should you inspect after solving? Check success and message, then use x and evaluate the residual when solution quality matters.
scipy.optimize.OptimizeResultscipy.optimize.show_optionsscipy.optimize.root_scalarscipy.optimize.fsolve