---
title: Discretizing continuous systems
heading: Turning a differential equation into a difference equation
eyebrow: C9 · Lesson 46 · Discretizing continuous systems
xp: 40
---

::: hook
Controllers are designed with Laplace transforms, but they run on microcontrollers that see the world one sample at a time. Simulators face the same problem in reverse: a circuit is a differential equation, and a computer can only step it. Every conversion from continuous to discrete time is an approximation of how the system behaves between samples, and the wrong one can make a perfectly stable system blow up.
:::

::: play
- The smooth curve is a first-order system's step response, $1 - e^{-at}$, plotted against the sample index n, with t = nT.
- The stems are two discrete versions. **ZOH** (zero-order hold) gives the exact values at every sample. **Forward Euler**, which steps along the slope, wanders off as aT grows.
- Push aT past 1: Euler overshoots and rings. Past 2 it diverges, although the real system is perfectly stable.
:::

```sim plot
view: graph
x: n = 0..20
y: y = -0.5..2
slider: aT = 0.1..2.4 step 0.05 = 0.5
plot: 1 - exp(-aT*n) | label: continuous
plot: 1 - exp(-aT*n) | stems | label: ZOH (exact at samples)
plot: 1 - (1 - aT)^n | stems | label: forward Euler
readout: ZOH pole = exp(-aT) | digits: 4
readout: Euler pole = 1 - aT | digits: 4
```

::: predict
## A first-order plant with a = 50 s⁻¹ is simulated by forward Euler with a 50 ms step. What happens?

- [ ] It settles, a little slowly
- [ ] It settles with some overshoot
- [x] It oscillates with growing amplitude
- [ ] It settles exactly as the real plant does

::: reveal
It diverges. aT = 2.5, so Euler's pole is 1 − 2.5 = −1.5, outside the unit circle: each step flips the sign and grows by 1.5. Forward Euler is stable only for aT < 2, here a step under 40 ms, and accurate only for aT well under 1.
:::
:::

::: nameit
## Pole mapping

Sampling a continuous mode $e^{st}$ every T seconds gives $(e^{sT})^n$, so a continuous pole at s becomes a discrete pole at

$$z = e^{sT}$$

The left half-plane maps inside the unit circle. A pole at $s = -\sigma \pm j\omega$ lands at radius $e^{-\sigma T}$ and angle $\pm\omega T$.

## Methods

| Method | Replace s with | Keeps |
|---|---|---|
| zero-order hold | exact solution with the input held constant between samples | the step response at the samples |
| forward Euler | $(z - 1)/T$ | nothing exactly; unstable for large T |
| backward Euler | $(z - 1)/(Tz)$ | stability, always (it over-damps) |
| Tustin (bilinear) | $\frac{2}{T}\cdot\frac{z - 1}{z + 1}$ | stability and the frequency response's shape (lesson 42) |

For $H(s) = a/(s + a)$, the ZOH equivalent is

$$H(z) = \frac{1 - e^{-aT}}{z - e^{-aT}}, \qquad y[n] = e^{-aT}y[n-1] + (1 - e^{-aT})u[n-1]$$

For a state-space model $\dot x = Ax + Bu$, the ZOH equivalent is $A_d = e^{AT}$ and $B_d = A^{-1}(e^{AT} - I)B$.

## The hold costs phase

A controller that outputs a held value updates T seconds apart; on average its output is T/2 late. That half-sample delay adds a phase lag of $\omega T/2$, which eats into the loop's phase margin. The usual rule is to sample at least 10 to 30 times faster than the closed loop's bandwidth.
:::

::: example
## Discretize $H(s) = 1/(s + 1)$ with a zero-order hold at T = 0.1 s.

::: solution
1. **Discrete pole.**
   $$\begin{aligned} e^{-aT} &= e^{-0.1} \\ &= 0.9048 \end{aligned}$$
2. **Transfer function.**
   $$H(z) = 0.0952/(z - 0.9048)$$
3. **Recursion.**
   $$\begin{aligned} y[n] &= 0.9048\,y[n-1] \\ &\quad + 0.0952\,u[n-1] \end{aligned}$$
4. **Check.** Its DC gain is 0.0952/(1 − 0.9048) = 1, as the original's is, and its step response matches $1 - e^{-t}$ exactly at every sample.

**Answer:** $H(z) = 0.0952/(z - 0.9048)$, or $y[n] = 0.9048\,y[n-1] + 0.0952\,u[n-1]$.
:::
:::

::: example
## A plant with a = 50 s⁻¹ is simulated with forward Euler. What step size keeps it stable, and what keeps it accurate to a few percent?

::: solution
1. **Stability.** Stability needs aT < 2, so T < 40 ms.
2. **Accuracy.** The Euler pole should be close to the true one. At aT = 0.1:
   $$\begin{aligned} 1 - aT &= 0.9 \\ e^{-aT} &= 0.905 \end{aligned}$$
3. **Step for accuracy.** That difference compounds to a few percent in the time constant, so T ≈ 2 ms.
4. **Stable or right.** Stable is a much weaker requirement than right.

**Answer:** T < 40 ms keeps it stable, but it takes T ≈ 2 ms to be accurate to a few percent.
:::
:::

::: example
## A digital control loop crosses over at 100 Hz and samples at 2 kHz. How much phase does the hold cost at crossover?

::: solution
1. **Hold delay.** The hold's average delay is
   $$T/2 = 0.25\ \text{ms}$$
2. **Phase lag at crossover.**
   $$360° \times 100 \times 0.00025 = 9°$$
3. **Effect on margin.** A loop designed in continuous time with 45° of margin keeps only about 36°.
4. **Faster sampling.** Sampling at 10 kHz would cut the loss to 1.8°.

**Answer:** the hold costs 9° of phase at crossover.
:::
:::

::: check
kind: parts
given: a = 2 | 10 | 100; T = 0.001 | 0.01 | 0.05

## $H(s) = \given{a}/(s + \given{a})$ is discretized with a zero-order hold at T = {T} s.

::: part
answer: exp(-a*T)
tolerance: 0.5%

### Where is the discrete pole?

::: misses
- [any] $e^{-aT}$.
:::
:::

::: part
answer: 1 - exp(-a*T)
tolerance: 0.5%

### What is the coefficient on u[n − 1]?

::: misses
- [any] $1 - e^{-aT}$, for a DC gain of 1.
:::
:::
:::

::: check
kind: parts
given: a = 5 | 20 | 300

## A plant pole is at s = −{a}, simulated by forward Euler.

::: part
answer: 2/a*1000
unit: ms
tolerance: 0.5%

### What is the largest step that keeps it stable?

::: misses
- [1/a*1000] Stability allows aT up to 2.
- [any] $T < 2/a$.
:::
:::

::: part
answer: 1 - a*0.001
tolerance: 0.5%

### What is the Euler pole with a 1 ms step?

::: misses
- [any] $1 - aT$.
:::
:::
:::

::: check
kind: parts
given: s = 1 | 10 | 50; w = 10 | 100; T = 0.001 | 0.01

## A continuous pole pair at $-\given{s} \pm j\given{w}$ is sampled every {T} s.

::: part
answer: exp(-s*T)
tolerance: 0.5%

### What radius do the discrete poles have?

::: misses
- [any] $e^{-\sigma T}$.
:::
:::

::: part
answer: w*T
unit: rad
tolerance: 0.5%

### At what angle?

::: misses
- [any] $\omega T$.
:::
:::
:::

::: check
kind: parts
given: f = 20 | 100 | 500; fs = 1 | 2 | 10

## A loop crosses over at {f} Hz and samples at {fs} kHz.

::: part
answer: 360*f/(fs*1000)/2
unit: °
tolerance: 0.5%

### How much phase does the hold's half-sample delay cost at crossover?

::: misses
- [360*f/(fs*1000)] The average delay is half a sample.
- [any] $360° \cdot f \cdot T/2$.
:::
:::

::: part
answer: fs*1000/f
tolerance: 0.5%

### How many samples per period of the crossover frequency is that?

::: misses
- [any] $f_s/f$.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.1..100, T = 0.0001..0.1, y = -10..10

## Apply forward Euler to $\dot y = -ay$ with step T, starting from y[n] = y.

::: step
answer: y - a*T*y

### What is y[n + 1]?
:::

::: step
answer: 1 - a*T

### What factor multiplies y each step?
:::

::: step
answer: 2/a

### Below what T does the factor stay between −1 and 1?
:::

::: right
Right. Forward Euler is stable only while $aT < 2$.
:::
:::

::: check
kind: choice

## Which discretization reproduces a system's step response exactly at the sample instants?

- [ ] Forward Euler
- [ ] Tustin
- [x] Zero-order hold
- [ ] Backward Euler

::: misses
- [1] It follows the slope at the start of each step and drifts.
- [2] Tustin keeps the frequency response's shape, not the step samples.
- [4] It's stable for any step, but not exact.
:::
:::

::: explain
## Why can forward Euler turn a stable system unstable, when zero-order hold never does?

::: model
Zero-order hold solves the differential equation exactly over each step, assuming only that the input stays constant, so each continuous pole s becomes $e^{sT}$, which is inside the unit circle whenever s is in the left half-plane, for any step size. Forward Euler instead assumes the slope at the start of a step holds for the whole step. For a fast mode and a long step that overshoots: $\dot y = -ay$ becomes $y[n+1] = (1 - aT)y[n]$, and once aT passes 2 the factor is below −1, so each step overshoots the equilibrium by more than the last. The real system decays; the approximation diverges, purely because the step was too long for the dynamics.
:::
:::

::: practice
::: check
given: a = 1 | 5; T = 0.02 | 0.2
answer: exp(-a*T)
tolerance: 0.5%

## ZOH pole of $\given{a}/(s + \given{a})$ at T = {T} s?

::: misses
- [any] $e^{-aT}$.
:::
:::

::: check
given: a = 10 | 40 | 100 | 250 | 400 | 1000 | 2000
answer: 2/a*1000
unit: ms
tolerance: 0.5%

## Largest forward-Euler step for a pole at −{a} s⁻¹?

::: misses
- [any] $2/a$.
:::
:::

::: check
given: f = 50 | 200; fs = 4 | 20
answer: 360*f/(fs*1000)/2
unit: °
tolerance: 0.5%

## Phase lost to the hold at {f} Hz, sampling at {fs} kHz?

::: misses
- [any] $360 f T/2$.
:::
:::
:::
