TExcelTAD
Home › Numerical Methods › Newton's Method for Systems

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 f:Rn→Rn\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^n, linearize around xk\mathbf{x}_k:

f(xk+s)≈f(xk)+J(xk) s,Jij=∂fi∂xj.\mathbf{f}(\mathbf{x}_k + \mathbf{s}) \approx \mathbf{f}(\mathbf{x}_k) + J(\mathbf{x}_k)\,\mathbf{s}, \qquad J_{ij} = \frac{\partial f_i}{\partial x_j}.

Setting the linear model to zero gives one Newton step:

  1. Solve J(xk) sk=−f(xk)J(\mathbf{x}_k)\,\mathbf{s}_k = -\mathbf{f}(\mathbf{x}_k).
  2. Set xk+1=xk+sk\mathbf{x}_{k+1} = \mathbf{x}_k + \mathbf{s}_k.

Never form J−1J^{-1}; solve the system (LU Factorization). Near a root with nonsingular Jacobian, convergence is quadratic.

python · try it yourself
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 JJ by hand is error-prone. A finite-difference approximation uses nn extra evaluations of f\mathbf{f}:

J:,j≈f(x+hej)−f(x)h,h≈u max⁡(1,∣xj∣).J_{:,j} \approx \frac{\mathbf{f}(\mathbf{x} + h\mathbf{e}_j) - \mathbf{f}(\mathbf{x})}{h}, \qquad h \approx \sqrt{u}\,\max(1, \lvert x_j\rvert).
python · try it yourself
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 (n2n^2 partial derivatives, or nn evaluations by finite differences) plus an O(n3)O(n^3) linear solve. For large nn 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 ∥f∥\lVert\mathbf{f}\rVert decreases enough.

Newton for optimization

Minimizing ϕ(x)\phi(\mathbf{x}) means solving ∇ϕ(x)=0\nabla\phi(\mathbf{x}) = \mathbf{0}. Newton's method on the gradient uses the Hessian as the Jacobian: H s=−∇ϕH\,\mathbf{s} = -\nabla\phi. 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; fsolve is 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 f(x,y)=(xy−1, x2−y)\mathbf{f}(x, y) = (xy - 1,\ x^2 - y).

Answer

(yx2x−1)\begin{pmatrix} y & x \\ 2x & -1\end{pmatrix}.

Solution

Row ii holds the partial derivatives of fif_i: ∂x(xy−1)=y\partial_x(xy - 1) = y, ∂y=x\partial_y = x; ∂x(x2−y)=2x\partial_x(x^2 - y) = 2x, ∂y=−1\partial_y = -1.

Exercise 2: One step by hand

Do one Newton step for the system of exercise 1 from (x,y)=(1,2)(x, y) = (1, 2).

Answer

s=(0,−1)\mathbf{s} = (0, -1), so x1=(1,1)\mathbf{x}_1 = (1, 1), which is already the exact root.

Solution

f(1,2)=(1,−1)\mathbf{f}(1, 2) = (1, -1), J=(212−1)J = \begin{pmatrix} 2 & 1 \\ 2 & -1\end{pmatrix}. Solve Js=−fJ\mathbf{s} = -\mathbf{f}, that is 2s1+s2=−12s_1 + s_2 = -1 and 2s1−s2=12s_1 - s_2 = 1. Adding gives 4s1=04s_1 = 0, so s1=0s_1 = 0 and s2=−1s_2 = -1. Then x1=(1,1)\mathbf{x}_1 = (1, 1), and indeed 1⋅1−1=01 \cdot 1 - 1 = 0 and 12−1=01^2 - 1 = 0.

Exercise 3: A three-equation system (code)

Solve x+y+z=6x + y + z = 6, xyz=6xyz = 6, x2+y2−z=2x^2 + y^2 - z = 2 by Newton from (1.2,1.8,3.3)(1.2, 1.8, 3.3) with a finite-difference Jacobian, and check with scipy.optimize.root.

Answer

Both reach the root (1,2,3)(1, 2, 3), Newton with a residual near machine precision.

Solution
python · try it yourself
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…

  1. the gradient
  2. the Jacobian matrix
  3. the Hessian always
  4. a scalar step size

How should the Newton step s=−J−1f\mathbf{s} = -J^{-1}\mathbf{f} be computed?

  1. Invert JJ and multiply
  2. Solve the linear system Js=−fJ\mathbf{s} = -\mathbf{f}
  3. Use the transpose
  4. Divide elementwise

A finite-difference Jacobian of an nn-variable function costs…

  1. 1 evaluation
  2. nn extra evaluations
  3. n2n^2 evaluations
  4. n3n^3 evaluations