Newton's Method for Systems
Many problems involve several unknowns and several equations at once: the equilibrium of a mechanism, the intersection of curves, a nonlinear circuit, one step of an implicit ODE solver. Newton's method generalizes directly, with the derivative replaced by the Jacobian matrix and the division replaced by a linear solve.
The iteration
For , linearize around :
Setting the linear model to zero gives one Newton step:
- Solve .
- Set .
Never form ; solve the system (LU Factorization). Near a root with nonsingular Jacobian, convergence is quadratic.
import numpy as np
def f(v):
x, y = v
return np.array([x**2 + y**2 - 4, # circle of radius 2
np.exp(x) + y - 1]) # curve y = 1 - e^x
def J(v):
x, y = v
return np.array([[2 * x, 2 * y],
[np.exp(x), 1.0]])
x = np.array([1.0, -1.5])
for k in range(7):
s = np.linalg.solve(J(x), -f(x))
x = x + s
print(f"k = {k + 1}: x = {np.round(x, 12)}, |f| = {np.linalg.norm(f(x)):.1e}")k = 1: x = [ 1.00937036 -1.7437531 ], |f| = 6.0e-02
k = 2: x = [ 1.00419942 -1.72968442], |f| = 2.3e-04
k = 3: x = [ 1.00416874 -1.72963729], |f| = 3.4e-09
k = 4: x = [ 1.00416874 -1.72963729], |f| = 9.9e-16
k = 5: x = [ 1.00416874 -1.72963729], |f| = 9.2e-16
k = 6: x = [ 1.00416874 -1.72963729], |f| = 0.0e+00
k = 7: x = [ 1.00416874 -1.72963729], |f| = 0.0e+00
Finite-difference Jacobians
Writing by hand is error-prone. A finite-difference approximation uses extra evaluations of :
import numpy as np
def fd_jacobian(f, x, f0=None):
f0 = f(x) if f0 is None else f0
Jm = np.empty((len(f0), len(x)))
for j in range(len(x)):
h = 1.49e-8 * max(1.0, abs(x[j]))
xp = x.copy(); xp[j] += h
Jm[:, j] = (f(xp) - f0) / h
return Jm
f = lambda v: np.array([v[0]**2 + v[1]**2 - 4, np.exp(v[0]) + v[1] - 1])
x = np.array([1.0, -1.5])
exact = np.array([[2.0, -3.0], [np.e, 1.0]])
print("max difference from exact Jacobian:", f"{np.max(np.abs(fd_jacobian(f, x) - exact)):.1e}")max difference from exact Jacobian: 1.3e-08
The approximation is good to about 8 digits, enough for Newton to converge (superlinearly rather than exactly quadratically).
Cost and robustness
Each step costs a Jacobian ( partial derivatives, or evaluations by finite differences) plus an linear solve. For large that motivates:
- reusing the same Jacobian for several steps (chord or modified Newton);
- updating an approximate Jacobian cheaply (Quasi-Newton Methods);
- automatic differentiation for exact Jacobians;
- line search or trust regions to make convergence global: accept a step only if decreases enough.
Newton for optimization
Minimizing means solving . Newton's method on the gradient uses the Hessian as the Jacobian: . This is the core of second-order optimization (Optimization).
In software
scipy.optimize.root(f, x0, jac=J, method="hybr")uses MINPACK's hybrid Powell method (Newton plus trust region);method="lm"is Levenberg-Marquardt;fsolveis the older interface.- JAX and PyTorch compute exact Jacobians by automatic differentiation (
jax.jacfwd,torch.autograd.functional.jacobian).
Exercises
Exercise 1: The Jacobian
Write the Jacobian of .
Answer
.
Solution
Row holds the partial derivatives of : , ; , .
Exercise 2: One step by hand
Do one Newton step for the system of exercise 1 from .
Answer
, so , which is already the exact root.
Solution
, . Solve , that is and . Adding gives , so and . Then , and indeed and .
Exercise 3: A three-equation system (code)
Solve , , by Newton from with a finite-difference Jacobian, and check with scipy.optimize.root.
Answer
Both reach the root , Newton with a residual near machine precision.
Solution
import numpy as np
from scipy.optimize import root
f = lambda v: np.array([v.sum() - 6, v.prod() - 6, v[0]**2 + v[1]**2 - v[2] - 2])
x = np.array([1.2, 1.8, 3.3])
for _ in range(20):
f0 = f(x)
Jm = np.empty((3, 3))
for j in range(3):
xp = x.copy(); xp[j] += 1e-7
Jm[:, j] = (f(xp) - f0) / 1e-7
x = x - np.linalg.solve(Jm, f0)
print(np.round(x, 10), f"{np.linalg.norm(f(x)):.1e}")
print(np.round(root(f, [1.2, 1.8, 3.3]).x, 10))[1. 2. 3.] 0.0e+00
[1. 2. 3.]
Test yourself
In Newton's method for systems, the derivative is replaced by…
- the gradient
- the Jacobian matrix
- the Hessian always
- a scalar step size
How should the Newton step be computed?
- Invert and multiply
- Solve the linear system
- Use the transpose
- Divide elementwise
A finite-difference Jacobian of an -variable function costs…
- 1 evaluation
- extra evaluations
- evaluations
- evaluations