Newton for systems of equations

Part 2 — Solving nonlinear equations

Numerical analysis for biologists

About this lecture

So far we solved one equation in one unknown. Biology rarely stops there: two genes regulate each other, so their steady state is two equations in two unknowns, solved together.

  • Partial derivatives: the slope in one direction while the other variables are held fixed. New for most of you; we go slowly, with numbers.
  • The Jacobian: all partial derivatives in one matrix. It plays the role of f'(x).
  • Newton for systems: linearise, then do one linear solve per step, solve(J, -F). Still quadratic convergence.
  • A finite-difference Jacobian when you cannot (or do not want to) differentiate by hand, and the library routine rootSolve::multiroot.
  • Biology: the genetic toggle switch. It has three steady states (bistability), and which one Newton finds depends on where it starts.

A biology problem: the toggle switch

Two genes whose proteins repress each other (Gardner, Cantor & Collins, Nature 2000). Concentrations u, v (in units of the repression threshold), time in units of the protein lifetime:

\frac{du}{dt} = \frac{\alpha}{1 + v^{n}} - u, \qquad \frac{dv}{dt} = \frac{\alpha}{1 + u^{n}} - v, \qquad \alpha = 3,\; n = 2

  • First term: production, a Hill function that falls as the repressor rises. Second term: degradation and dilution.
  • \alpha = promoter strength, n = Hill coefficient (cooperativity).

Steady states: two equations, two unknowns

A steady state has du/dt = 0 and dv/dt = 0:

\begin{aligned} F_1(u, v) &= \frac{3}{1 + v^2} - u = 0\\[2pt] F_2(u, v) &= \frac{3}{1 + u^2} - v = 0 \end{aligned}

  • Each equation alone is a curve. Steady states are where the curves cross.
  • Here: three crossings. Two “decided” states (U on/V off, V on/U off) and one in between.
  • Two stable states = bistability: a memory switch.

Why a new method?

alpha <- 3; n <- 2
F <- function(x) {
  u <- x[1]; v <- x[2]
  c(alpha / (1 + v^n) - u,
    alpha / (1 + u^n) - v)
}
F(c(2.618, 0.382))          # close to the "U on" state?
# [1] -2.533501e-05 -2.533358e-05
F(c(1, 1))
# [1] 0.5 0.5
  • With 2 genes we could substitute one equation into the other and use a 1-D method. With 20 genes we cannot.
  • We could simulate the ODE until it stops moving. That is slow near a steady state, and it never finds the unstable middle one.
  • Newton generalises directly: one method for any number of equations.

Recap: Newton in one variable

Replace f by its tangent line at x_k and solve the line instead:

f(x_k + \delta) \approx f(x_k) + f'(x_k)\,\delta = 0 \quad\Rightarrow\quad \delta = -\frac{f(x_k)}{f'(x_k)}, \qquad x_{k+1} = x_k + \delta

Example from Newton and secant methods: the EC50 of the two-site dose–response curve, f(c) = R(c) - 0.5, from c_0 = 1:

k c_k f(c_k) f'(c_k) error \lvert c_k - c_{50}\rvert
0 1 -0.200 0.0766 2.26
1 3.61661 0.0324 0.0895 3.6\times10^{-1}
2 3.25482 -6.0\times10^{-4} 0.0926 6.5\times10^{-3}
3 3.2613037 -1.4\times10^{-7} 0.0925 1.6\times10^{-6}
4 3.26130527 -8.4\times10^{-15} 0.0925 9.1\times10^{-14}

To do the same for \mathbf F(u, v) we need the “derivative” of a function of two variables that returns two numbers.

Partial derivatives: hold the others fixed

F_1(u, v) = \dfrac{3}{1+v^2} - u depends on two numbers. Ask one question at a time:

  • How fast does F_1 change when only u moves? Treat v as a constant and differentiate in u as usual: \frac{\partial F_1}{\partial u} = -1
  • How fast does F_1 change when only v moves? Treat u as a constant (it drops out) and use the chain rule on 3(1+v^2)^{-1}: \frac{\partial F_1}{\partial v} = -\frac{3 \cdot 2v}{(1+v^2)^2} = -\frac{6v}{(1+v^2)^2}
  • At the point (u, v) = (3, 0.5): \dfrac{\partial F_1}{\partial v} = -\dfrac{6 \cdot 0.5}{1.25^2} = -\dfrac{3}{1.5625} = -1.92.

The curly \partial (“partial”) just means: other variables held fixed.

Partial derivatives are slopes of slices

f1 <- function(u, v) alpha / (1 + v^2) - u
h <- 1e-6
(f1(3 + h, 0.5) - f1(3, 0.5)) / h      # move u only
# [1] -1
(f1(3, 0.5 + h) - f1(3, 0.5)) / h      # move v only
# [1] -1.92

The Jacobian: all partial derivatives in one matrix

Row i = equation F_i, column j = variable x_j:

J = \begin{pmatrix} \dfrac{\partial F_1}{\partial u} & \dfrac{\partial F_1}{\partial v} \\[8pt] \dfrac{\partial F_2}{\partial u} & \dfrac{\partial F_2}{\partial v}\end{pmatrix}

\phantom{J} = \begin{pmatrix} -1 & -\dfrac{6v}{(1+v^2)^2} \\[8pt] -\dfrac{6u}{(1+u^2)^2} & -1\end{pmatrix}

At (3, 0.5): \dfrac{6\cdot 3}{(1+9)^2} = 0.18 and \dfrac{6\cdot0.5}{1.25^2} = 1.92.

J <- function(x) {
  u <- x[1]; v <- x[2]
  matrix(c(-1, -alpha * n * v^(n - 1) / (1 + v^n)^2,
           -alpha * n * u^(n - 1) / (1 + u^n)^2, -1),
         nrow = 2, byrow = TRUE)
}
J(c(3, 0.5))
#       [,1]  [,2]
# [1,] -1.00 -1.92
# [2,] -0.18 -1.00

The linear approximation, with numbers

One variable: f(x + \delta) \approx f(x) + f'(x)\,\delta. Two variables, one line per equation:

\begin{aligned} F_1(u + \delta_u, v + \delta_v) &\approx F_1 + \tfrac{\partial F_1}{\partial u}\delta_u + \tfrac{\partial F_1}{\partial v}\delta_v\\ F_2(u + \delta_u, v + \delta_v) &\approx F_2 + \tfrac{\partial F_2}{\partial u}\delta_u + \tfrac{\partial F_2}{\partial v}\delta_v \end{aligned}

In matrix form: \mathbf F(\mathbf x + \boldsymbol\delta) \approx \mathbf F(\mathbf x) + J\,\boldsymbol\delta (matrix J times vector \boldsymbol\delta).

x <- c(3, 0.5); d <- c(-0.1, 0.05)
F(x + d)                       # the true value
# [1] -0.5967370 -0.2311902
F(x) + J(x) %*% d              # the linear prediction
#        [,1]
# [1,] -0.596
# [2,] -0.232

The Newton step: one linear solve

Ask for \mathbf F(\mathbf x_k + \boldsymbol\delta) = \mathbf 0 in the linear approximation:

\mathbf F(\mathbf x_k) + J(\mathbf x_k)\,\boldsymbol\delta = \mathbf 0 \quad\Longleftrightarrow\quad \boxed{J(\mathbf x_k)\,\boldsymbol\delta = -\mathbf F(\mathbf x_k)}, \qquad \mathbf x_{k+1} = \mathbf x_k + \boldsymbol\delta

one variable system
f(x_k), a number \mathbf F(\mathbf x_k), a vector
f'(x_k), a number J(\mathbf x_k), a matrix
\delta = -f/f' (divide) solve J\boldsymbol\delta = -\mathbf F (linear system)
fails if f' = 0 fails if J is singular (\det J = 0)
  • Each step is a linear system, which we know how to solve (Gaussian elimination). In R: solve(J, -F).

One step by hand

Start \mathbf x_0 = (3, 0.5). We have \mathbf F(\mathbf x_0) = (-0.6, -0.2) and J from before. Solve

\begin{pmatrix} -1 & -1.92 \\ -0.18 & -1 \end{pmatrix}\begin{pmatrix}\delta_u\\ \delta_v\end{pmatrix} = \begin{pmatrix} 0.6 \\ 0.2 \end{pmatrix}

  • Row 2 minus 0.18\times row 1: (-1 + 0.3456)\,\delta_v = 0.2 - 0.108, so \delta_v = 0.092 / (-0.6544) = -0.1406.
  • Row 1: -\delta_u = 0.6 + 1.92\,\delta_v = 0.3301, so \delta_u = -0.3301.
  • \mathbf x_1 = (3 - 0.3301,\; 0.5 - 0.1406) = (2.6699,\; 0.3594).
delta <- solve(J(c(3, 0.5)), -F(c(3, 0.5)))
delta
# [1] -0.3300733 -0.1405868
F(c(3, 0.5) + delta)              # residual: was (-0.6, -0.2)
# [1] -0.013126396  0.009658204

Newton for systems in R

newton_sys <- function(F, J, x, tol = 1e-12, maxit = 50) {
  for (k in 1:maxit) {
    delta <- solve(J(x), -F(x))          # one linear solve per step
    x <- x + delta
    if (sqrt(sum(delta^2)) < tol) return(x)
  }
  warning("no convergence")
  x
}
print(newton_sys(F, J, c(3, 0.5)), digits = 15)
# [1] 2.618033988749895 0.381966011250105
  • sqrt(sum(delta^2)) is the length of the step vector, \|\boldsymbol\delta\|. Stop when the step is tiny; also check the residual \|\mathbf F\|.
  • Exact root (exercise): \big((3+\sqrt5)/2,\ (3-\sqrt5)/2\big). All 16 digits agree.

Still quadratic

Error \|\mathbf x_k - \mathbf x^*\| from (3, 0.5):

k error
0 4.0\times10^{-1}
1 5.7\times10^{-2}
2 2.1\times10^{-3}
3 2.9\times10^{-6}
4 5.9\times10^{-12}
5 < 10^{-16}
  • The exponent roughly doubles each step, as in 1-D: e_{k+1} \approx C\,e_k^2.
  • A dropping curve on a log axis that gets steeper = quadratic.

Three steady states: which one do we get?

starts <- rbind(c(3, 0.5), c(0.5, 3), c(1, 1), c(1, 0.2), c(4, 4))
t(apply(starts, 1, function(s) newton_sys(F, J, s)))   # one run per row
#          [,1]     [,2]
# [1,] 2.618034 0.381966       U on, V off
# [2,] 0.381966 2.618034       V on, U off
# [3,] 1.213412 1.213412       middle
# [4,] 0.381966 2.618034       V on: although U > V at the start!
# [5,] 1.213412 1.213412       middle
  • Bistable: two “decided” states, plus the middle one. Newton finds one root per start.
  • To find them all: run from many starts and collect the distinct answers.
  • The start (1, 0.2) has more U than V, yet Newton lands on “V on”. Newton’s choice is not the cell’s choice.

Where Newton goes vs where the cell goes

  • Left: colour = the steady state Newton converges to from that start. Right: where the ODE (the cell) ends up.
  • Newton’s regions (basins) are curved, with thin bands of all colours; the middle, unstable state takes 57 % of this square.

Try it: Newton in the plane

Bistability needs a strong promoter

Run Newton from a 7 × 7 grid of starts for each promoter strength \alpha, keep the distinct roots.

  • \alpha < 2: one steady state; the circuit is not a switch.
  • \alpha > 2: three; the circuit is bistable.
  • Designing the switch (Gardner et al.) meant tuning promoters until the circuit was in the bistable range.

No formula for J? Finite differences

Often \mathbf F is a long function (a 20-gene network, a model with many terms) and deriving J by hand is tedious and error-prone. Approximate column j by nudging only x_j:

J_{:,j} \approx \frac{\mathbf F(\mathbf x + h_j\,\mathbf e_j) - \mathbf F(\mathbf x)}{h_j}, \qquad h_j = \sqrt{\varepsilon}\,\max(|x_j|, 1) \approx 1.5\times10^{-8}\max(|x_j|, 1)

(\mathbf e_j = 1 in place j, 0 elsewhere.)

  • One nudge gives a whole column: how every equation responds to x_j.
  • Cost: n + 1 evaluations of \mathbf F per Jacobian.
  • h = \sqrt\varepsilon balances truncation and rounding error (Convergence and cost): about 8 correct digits.

Finite-difference Jacobian in R

fd_jacobian <- function(F, x,
                        h = sqrt(.Machine$double.eps)) {
  Fx <- F(x)
  Jm <- matrix(0, length(Fx), length(x))
  for (j in seq_along(x)) {
    hj <- h * max(abs(x[j]), 1)
    xh <- x; xh[j] <- x[j] + hj
    Jm[, j] <- (F(xh) - Fx) / hj     # column j
  }
  Jm
}
max(abs(fd_jacobian(F, c(3, 0.5)) - J(c(3, 0.5))))
# [1] 1.311302e-08

  • Use it also to check a hand-derived J: a wrong entry shows up as an error much larger than 10^{-7}.

The cost of a step: one linear solve

Per Newton step for n genes:

part cost
evaluate \mathbf F depends on the model, often \sim n
Jacobian by finite differences n + 1 evaluations of \mathbf F
solve J\boldsymbol\delta = -\mathbf F (Gaussian elimination) \approx \tfrac23 n^3 operations
  • n = 2: nothing. n = 1000: \approx 7\times10^8 per step, still under a second. n = 20\,000: hours, unless J is sparse (most genes do not regulate most others).
  • Never form solve(J) (the inverse) and multiply: solve(J, -F) is cheaper and more accurate.

The library routine: rootSolve::multiroot

library(rootSolve)
sol <- multiroot(f = F, start = c(3, 0.5))      # Jacobian by finite differences
sol$root;  sol$f.root;  sol$iter
# [1] 2.618034 0.381966
# [1] -1.471712e-12  8.891221e-13
# [1] 5
sol2 <- multiroot(f = F, start = c(3, 0.5), jacfunc = J)  # supply your own J
  • multiroot is Newton–Raphson with a finite-difference Jacobian (unless you pass jacfunc), plus tolerances (rtol, atol, ctol), an iteration limit and sparse-Jacobian options for large models.
  • It stops at its own tolerance: here \|\mathbf F\| \approx 10^{-12}, not 10^{-16}. Check f.root, and set atol tighter if you need it.
  • It finds one root, from your start. Multiple starts are still your job.

Failure 1: a nearly singular Jacobian

x0 <- c(1.9, 0.5)
det(J(x0))                          # close to 0
# [1] -0.02992175
x0 + solve(J(x0), -F(x0))           # the first step
# [1] -5.136430  4.425224           # negative concentration!
newton_sys(F, J, c(1.9, 0.5))       # ends in the middle state
newton_sys(F, J, c(2.0, 0.55))      # a nearby start: "U on"
  • \det J \approx 0: the linear model is nearly flat in some direction, so the step is huge. It lands at u < 0, which is not a concentration.
  • With a non-integer Hill coefficient (n = 2.5) the same kind of jump gives (-3.25)^{2.5} = NaN, and Newton returns NaN.
  • Check: watch \|\boldsymbol\delta\| and \det J (or the condition number); check all(is.finite(x)) and all(x >= 0).

Failure 2: a root, but not the one you wanted

  • Newton from (1, 1), (2, 2) or (4, 4) converges, quickly and correctly, to the middle state (1.2134, 1.2134): a start with u = v stays on the diagonal by symmetry.
  • The residual is 10^{-16}: nothing in the output warns you that this state is unstable, i.e. that no real cell sits there.
  • A single run hides the other two states completely: you would conclude the switch is not bistable.

Checks

  • Run from many starts (a grid, or random) and list the distinct roots.
  • Check each root for stability: eigenvalues of J at the root (Equilibria, stability and the phase plane), or a short ODE simulation from a nearby point.

Checklist for Newton on a system

  • Check J against a finite-difference Jacobian before anything else (difference \sim 10^{-8}, not \sim 1).
  • Watch the iteration: \|\mathbf F(\mathbf x_k)\| should fall fast (digits double). If it grows or stalls, the start is too far or J is wrong.
  • Look at the root: finite, non-negative, residual small (f.root in multiroot).
  • Large steps / tiny \det J → the start is near a singular Jacobian; try another start or a damped/trust-region solver.
  • Many starts → collect distinct roots; one run finds one root.
  • Stability is a separate question: a root of \mathbf F is not necessarily where the cell goes.

Summary

  • A partial derivative \partial F_i/\partial x_j is an ordinary derivative with all other variables held fixed. The Jacobian J collects them: row = equation, column = variable.
  • Linearise: \mathbf F(\mathbf x + \boldsymbol\delta) \approx \mathbf F(\mathbf x) + J\boldsymbol\delta. Newton step: solve J\boldsymbol\delta = -\mathbf F, set \mathbf x \leftarrow \mathbf x + \boldsymbol\delta. One linear solve per step: solve(J(x), -F(x)).
  • Convergence is quadratic near a root, as in one variable.
  • Finite-difference Jacobian: one column per nudge, h = \sqrt\varepsilon\max(|x_j|, 1), about 8 correct digits, n + 1 evaluations of \mathbf F.
  • Library: rootSolve::multiroot (R), scipy.optimize.root (Python).
  • The toggle switch has three steady states when \alpha > 2 (bistable). Which one Newton finds depends on the start, in complicated ways, and it finds unstable states as happily as stable ones.
  • Failures: nearly singular J (huge steps, negative concentrations, NaN), and converging to a root you did not want. Checks: step size, positivity, many starts.

Next: Part 3, Gaussian elimination as LU: what solve does inside, and what it costs.