---
title: Stiff equations and implicit methods
heading: Why SPICE takes its steps backward
eyebrow: A8 · Lesson 46 · Stiff equations
xp: 50
---

::: hook
A circuit has a 1 µs time constant from a small bypass capacitor and a 1 s time constant from a big reservoir. You want to watch the slow one for a few seconds. With Euler's method, you'd expect to take steps of a few milliseconds. Try it and the simulation explodes to $10^{90}$: the fast mode, long since decayed in the real circuit, comes back and grows without bound. Such problems are **stiff**, and every circuit simulator meets them constantly. The cure is an implicit method, which evaluates the slope at the end of each step instead of the start.
:::

::: play
- Both methods solve $\dot y = -y$ (time constant 1) with step size $h$. The smooth curve is the exact $e^{-t}$.
- Explicit (forward) Euler multiplies by $1 - h$ each step; implicit (backward) Euler divides by $1 + h$.
- Push $h$ past 1, then past 2. What happens to each?
:::
:::

```sim plot
view: graph
x: t = 0..10
y: y = -2..1.5
slider: h = 0.1..3 step 0.1 = 0.5
plot: exp(-t) | label: exact
plot: (1 - h)^floor(t/h) | label: forward Euler | color: ch2 | samples: 2000
plot: (1/(1 + h))^floor(t/h) | label: backward Euler | color: ch3 | samples: 2000
readout: forward factor per step = 1 - h | digits: 3
readout: backward factor per step = 1/(1 + h) | digits: 3
```

::: predict
## Forward Euler on $\dot y = -1000y$ with $h$ = 0.01. What happens?

- [ ] It's slightly inaccurate but fine
- [x] It blows up: each step multiplies by $1 - 10 = -9$
- [ ] It's exact
- [ ] It decays too fast

::: reveal
It blows up. The factor per step is $1 + h\lambda = 1 - 10 = -9$, so the error flips sign and grows ninefold every step. Forward Euler is stable only if $|1 + h\lambda| \le 1$, which needs $h \le 2/1000$ here, however little you care about the fast mode.
:::
:::

::: nameit
## Stability of a method

Apply a method to the test equation $\dot y = \lambda y$ ($\lambda < 0$). Each step multiplies $y$ by a factor $R(h\lambda)$; the method is stable when $|R| \le 1$.

| Method | Step | Factor $R$ | Stable for |
|---|---|---|---|
| forward (explicit) Euler | $y_{n+1} = y_n + hf(y_n)$ | $1 + h\lambda$ | $h \le \frac{2}{\lvert\lambda\rvert}$ |
| backward (implicit) Euler | $y_{n+1} = y_n + hf(y_{n+1})$ | $\frac{1}{1 - h\lambda}$ | every $h > 0$ |
| trapezoidal | average of the two | $\frac{1 + h\lambda/2}{1 - h\lambda/2}$ | every $h > 0$ |

## Stiffness

A system is **stiff** when its time constants span a huge range. The fastest mode sets the stability limit of an explicit method, even after it has decayed to nothing, while the slowest sets how long you must simulate. With time constants of 1 µs and 1 s, forward Euler needs about $\frac{5\text{ s}}{2\text{ µs}} = 2.5$ million steps.

## Implicit methods

Backward Euler for $\dot{\mathbf{x}} = A\mathbf{x}$ needs a solve at every step:

$$(I - hA)\mathbf{x}_{n+1} = \mathbf{x}_n$$

That costs more per step, but the step size is now limited only by the accuracy you want for the slow modes. For a linear circuit $I - hA$ is the same every step, so you factor it once (A7, lesson 9). SPICE uses the trapezoidal rule and Gear's methods, both implicit, for exactly this reason; for nonlinear circuits each step is a Newton solve.
:::

::: example
## A circuit has time constants of 1 µs and 1 s. How many forward Euler steps does it take to simulate 5 s?

::: solution
1. **Forward Euler's step limit.** Stability needs $h \le 2 \times 1$ µs $= 2$ µs.
2. **Count the steps.**
   $$\frac{5}{2 \times 10^{-6}} = 2.5 \times 10^6$$
   steps, all to resolve a mode that dies in the first few microseconds.
3. **Compare backward Euler.** Backward Euler with $h$ = 10 ms, accurate enough for the 1 s mode, needs 500.

**Answer:** forward Euler takes $2.5 \times 10^6$ steps; backward Euler needs only 500.
:::
:::

::: example
## Backward Euler on $\dot y = -1000y$ with $h$ = 0.01. What does each step do?

::: solution
1. **One step.**
   $$\begin{aligned} y_{n+1} &= \frac{y_n}{1 + 10} \\ &= \frac{y_n}{11} \end{aligned}$$
2. **What it does to the fast mode.** The fast mode is crushed by a factor of 11 every step instead of exploding.
3. **Compare the exact decay.** That's slower than the exact decay, $e^{-10}$ per step, but the mode is gone within a few steps either way, which is all that matters for a mode that has died out.

**Answer:** each step divides $y$ by 11, so the fast mode is gone within a few steps.
:::
:::

::: example
## Apply the trapezoidal rule to $\dot y = -1000y$ with $h$ = 0.01. What does each step do?

::: solution
1. **Write the step.** The trapezoidal rule averages the slopes at both ends:
   $$y_{n+1} = y_n - 5(y_n + y_{n+1})$$
2. **Solve for $y_{n+1}$.**
   $$\begin{aligned} y_{n+1} &= \frac{1 - 5}{1 + 5}y_n \\ &= -0.667y_n \end{aligned}$$
3. **Read the factor.** It's stable, since the factor is below 1 in size, but the sign flips every step: a slowly decaying oscillation that the true solution doesn't have.
4. **Compare backward Euler.** Backward Euler gives $\frac{1}{11} = 0.091$ per step, decaying smoothly. That's why simulators often take a few backward Euler steps after a sudden change before switching to the more accurate trapezoidal rule.

**Answer:** each step multiplies $y$ by −0.667: stable, but the sign flips every step.
:::
:::

::: check
given: tau = 1 | 10 | 0.5
answer: 2*tau
unit: µs

## A circuit's fastest time constant is {tau} µs. What is the largest step forward Euler can take without blowing up?

::: right
Right. $|1 + h\lambda| \le 1$ with $\lambda = -1/\tau$ needs $h \le 2\tau$ = {answer}.
:::

::: misses
- [tau] Up to $2\tau$ is stable, though steps above $\tau$ make it oscillate.
- [any] Not yet. Solve $|1 - h/\tau| \le 1$.
:::
:::

::: check
given: h = 0.5 | 2 | 5
answer: 1/(1 + h)
tolerance: 0.5%

## Backward Euler solves $\dot y = -y$ with $h$ = {h}. By what factor does $y$ shrink each step?

::: right
Right. $y_{n+1} = \frac{y_n}{1 + h}$, a factor of {answer}: always between 0 and 1.
:::

::: misses
- [1 - h] That's forward Euler.
- [exp(-h)] That's the exact factor; backward Euler gives $\frac{1}{1 + h}$.
- [any] Not yet. Solve $y_{n+1} = y_n - hy_{n+1}$.
:::
:::

::: check
given: tf = 1 | 10; ts = 1 | 5; T = 5 | 3
answer: T*ts/(2*tf*1e-6)

## A circuit has time constants {tf} µs and {ts} s. You need to simulate {T} slow time constants. How many forward Euler steps, at the stability limit?

::: right
Right. $\frac{T\tau_s}{2\tau_f}$ = {answer}.
:::

::: misses
- [T*ts/(tf*1e-6)] The limit is $h = 2\tau_f$.
- [any] Not yet. Total time over the largest stable step.
:::
:::

::: check
given: h = 2.5 | 3 | 4; n = 5 | 6
answer: (1 - h)^n

## Forward Euler solves $\dot y = -y$ with $y(0) = 1$ and $h$ = {h}. What is $y$ after {n} steps?

::: right
Right. $(1 - h)^n$ = {answer}: an exploding, sign-flipping sequence instead of a decay.
:::

::: misses
- [(1 + h)^n] The factor is $1 + h\lambda$ with $\lambda = -1$.
- [exp(-h*n)] That's the exact solution.
- [any] Not yet. Multiply by $1 - h$ each step.
:::
:::

::: check
kind: choice

## Why do circuit simulators like SPICE use implicit integration methods?

- [x] Circuits are stiff, and implicit methods stay stable with steps sized for the slow behavior
- [ ] Implicit methods are cheaper per step
- [ ] Explicit methods can't handle capacitors
- [ ] Implicit methods are exact

::: right
Right. Each implicit step costs a linear solve, but the steps can be thousands of times longer.
:::

::: misses
- [2] They cost more per step; they win on the number of steps.
- [4] They have errors like any method; they just don't blow up.
- [any] Think about time constants of very different sizes.
:::
:::

::: check
kind: python

## Write `backward_euler(A, x0, h, n)`, taking `n` backward Euler steps for $\dot{\mathbf{x}} = A\mathbf{x}$ and returning the final state. Factor or reuse $I - hA$; don't recompute it inside the loop.

```python starter
import numpy as np

def backward_euler(A, x0, h, n):
    x = x0.astype(float).copy()
    # each step solves (I - h A) x_new = x
    return x

A = np.array([[-1000.0, 0], [1, -1]])
print(backward_euler(A, np.array([1.0, 0]), 0.01, 100))
```

```python solution
import numpy as np

def backward_euler(A, x0, h, n):
    M = np.eye(len(x0)) - h * A
    x = x0.astype(float).copy()
    for _ in range(n):
        x = np.linalg.solve(M, x)
    return x

A = np.array([[-1000.0, 0], [1, -1]])
print(backward_euler(A, np.array([1.0, 0]), 0.01, 100))
```

```python tests
import numpy as np
A = np.array([[-1000.0, 0], [1, -1]])
x = backward_euler(A, np.array([1.0, 0]), 0.01, 100)
exact = (np.exp(-1) - np.exp(-1000)) / 999
assert abs(x[0]) < 1e-50, "The fast mode should be crushed to nothing."
assert abs(x[1] - exact) / exact < 0.02, f"x2(1) should be close to {exact:.4e}; got {x[1]:.4e}."
y = backward_euler(np.array([[-1.0]]), np.array([1.0]), 0.5, 4)
assert np.isclose(y[0], (1 / 1.5) ** 4), "On y' = -y with h = 0.5, each step divides by 1.5."
```

::: right
Right. With $h$ = 0.01 the fast mode would make forward Euler explode by a factor of 9 per step; backward Euler crushes it and tracks the slow mode to about 0.5%.
:::
:::

::: check
kind: parts
given: l = 100 | 1000; h = 0.03 | 0.1

## $\dot y = -\given{l}y$ is stepped with $h$ = {h}.

::: part
answer: 1 - l*h
tolerance: 0.5%

### What factor does forward Euler multiply by each step?

::: misses
- [1/(1 + l*h)] That's backward Euler.
- [any] $1 + \lambda h$.
:::
:::

::: part
answer: (1 - l*h/2)/(1 + l*h/2)
tolerance: 0.5%

### What factor does the trapezoidal rule multiply by?

::: misses
- [any] $\frac{1 + \lambda h/2}{1 - \lambda h/2}$.
:::
:::
:::

::: check
kind: derivation
vars: f = 0.001..0.1, T = 1..10

## A circuit has time constants $f$ (fast) and $T$ (slow), both in seconds, and is simulated for $5T$.

::: step
answer: 2*f

### What is the largest stable forward Euler step?
:::

::: step
answer: 5*T/(2*f)

### How many forward Euler steps does the run take?
:::

::: right
Right. The fast mode, long since dead, still sets the step: that's stiffness.
:::
:::

::: explain
## Why does the fastest time constant limit forward Euler's step size even after that mode has decayed away?

::: model
Forward Euler multiplies each mode by $1 + h\lambda$ every step. Rounding errors and the tiny remnants of the fast mode never become exactly zero, so if $|1 + h\lambda_\text{fast}| > 1$, those remnants are amplified every step and eventually swamp the solution. The method has no way to know the mode is irrelevant; stability is decided by the eigenvalues, not by how much of each mode is present. Implicit methods have factors below 1 in size for every decaying mode, so the step size can be chosen for the slow behavior alone.
:::
:::

::: practice

::: check
given: h = 0.05 | 0.1 | 0.2 | 0.25 | 0.5 | 1
answer: (1 - h/2)/(1 + h/2)
tolerance: 0.5%

## The trapezoidal rule solves $\dot y = -y$ with $h$ = {h}. What is its factor per step?

::: misses
- [any] $\frac{1 + h\lambda/2}{1 - h\lambda/2}$ with $\lambda = -1$.
:::
:::

::: check
given: lam = 50 | 100 | 200 | 500 | 1000 | 2000
answer: 2/lam
unit: s
tolerance: 0.5%

## What is the largest stable forward Euler step for $\dot y = -\given{lam}y$?

::: misses
- [1/lam] Up to $2/|\lambda|$ is stable.
- [any] $|1 - h\lambda| \le 1$.
:::
:::

::: check
given: ts = 1 | 2; tf = 0.001 | 0.0001
answer: ts/tf

## What is the stiffness ratio of a system with time constants {ts} s and {tf} s?

::: misses
- [tf/ts] Slow over fast.
- [any] $\tau_\text{slow}/\tau_\text{fast}$.
:::
:::
:::
