Scalar root finding
Solving a scalar nonlinear equation when an approximate root is available.
SciPy · Function
Finds a zero of a real or complex scalar function with Newton-Raphson, secant, or Halley iteration. The selected method depends on whether first- and second-derivative callables are supplied.
newton iteratively seeks a zero of a scalar-valued function. Providing its first derivative selects Newton-Raphson, omitting it selects the secant method, and providing both first- and second-derivative functions selects Halley’s method. A scalar starting value returns one root; multiple starting values are processed elementwise.
scipy.optimize.newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None, x1=None, rtol=0.0, full_output=False, disp=True)| Name | Type | Description |
|---|---|---|
func | callable | The function whose root is sought. |
x0 | float, complex, or array_like | An initial estimate of the root. If x0 is a sequence, the method is applied to each element. |
fprime | callable, optional | The first derivative of func. If provided, Newton-Raphson is used. |
args | tuple, optional | Extra positional arguments passed to func, fprime, and fprime2. |
tol | float, optional | Absolute tolerance for the root; the algorithm stops when the step meets the documented tolerance test. |
maxiter | int, optional | Maximum number of iterations. |
fprime2 | callable, optional | The second derivative of func. If provided together with fprime, Halley’s method is used. |
x1 | float or array_like, optional | A second starting point for the secant method when fprime is not supplied. |
rtol | float, optional | Relative tolerance for the root. |
full_output | bool, optional | If True, return additional convergence information. |
disp | bool, optional | If True, raise RuntimeError when the algorithm fails to converge. |
A scalar root for scalar x0, or an array of roots for multiple starting values. With full_output=True and scalar x0, returns the root plus a RootResults object; with array-like x0, returns the roots, a convergence mask, and a zero-derivative mask.
import numpy as np
from scipy.optimize import newton
root = newton(lambda x: np.cos(x) - x, 0.5)
print(root)Finds the fixed point of cosine starting near 0.5.
Solving a scalar nonlinear equation when an approximate root is available.
Solving several independent scalar equations using a collection of starting estimates.
Accelerating local convergence when analytic first and second derivatives are available.
The function solves f(x)=0 from a nearby starting value. Supplying fprime uses Newton-Raphson, omitting it uses the secant method, and supplying fprime2 as well uses Halley’s method.
Follow-up: What changes for multiple initial values? A sequence x0 produces an array of roots, with convergence and zero-derivative information returned when full_output is enabled.
func must accept the current scalar x as its first argument. Extra fixed arguments are passed after x through args, so a function can be written as f(x, a, b).
Follow-up: How can derivative functions receive those values? Pass the same args tuple; SciPy calls fprime and fprime2 with x followed by those extra arguments.
tol controls the absolute tolerance for the iterate change, while rtol controls the relative tolerance. maxiter limits the number of iterations, and the default maximum is 50.
Follow-up: Why should users validate convergence? The documentation notes that the stopping test is based on step size and does not guarantee that the returned value is a root.
scipy.optimize.root_scalarscipy.optimize.brentqscipy.optimize.fixed_pointscipy.optimize.newton_krylov