---
title: Differential and difference equations
heading: Systems described by their equations and block diagrams
eyebrow: C9 · Lesson 9 · Differential and difference equations
xp: 40
---

::: hook
Most LTI systems aren't handed to you as an impulse response. A circuit comes as a differential equation from Kirchhoff's laws; a digital filter comes as a few lines of code that compute each output from recent inputs and outputs. That line of code is a **difference equation**, and running it is a recursion: the output feeds back into its own next value. A two-line recursion can ring like a tuning fork, and seeing how its coefficients set the ringing is the first step toward the z-transform.
:::

::: play
- The stems are the impulse response of $y[n] = 2r\cos\theta\,y[n-1] - r^2 y[n-2] + x[n]$: two multiplies and two adds per sample.
- θ sets how fast it oscillates (a full cycle every 2π/θ samples) and r how fast it dies: by a factor r per sample.
- Push r to 1: the ringing never stops. Past 1 it grows: the recursion has become unstable.
:::
:::

```sim plot
view: graph
x: n = 0..40
y: h = -2..2
slider: r = 0.5..1.05 = 0.9 step 0.01
slider: theta = 0.1..3 = 1.047 step 0.01
plot: r^n*sin((n + 1)*theta)/sin(theta) | stems | label: h[n]
readout: feedback a1 = 2*r*cos(theta) | digits: 3
readout: feedback a2 = -r^2 | digits: 3
readout: samples per cycle = 2*pi/theta | digits: 3
```

::: predict
## $y[n] = 0.5\,y[n-1] + x[n]$ starts at rest and is fed a single impulse, x[0] = 1. What is y[3]?

- [ ] 0
- [ ] 0.5
- [x] 0.125
- [ ] 1.5

::: reveal
0.125. y[0] = 1, then each output is half the one before: 0.5, 0.25, 0.125. The impulse response is $0.5^n u[n]$, infinitely long though the equation is one line. That's an **IIR** (infinite impulse response) system.
:::
:::

::: nameit
## Difference equations

A causal LTI digital filter computes

$$y[n] = \sum_{k=1}^{N} a_k\,y[n-k] + \sum_{k=0}^{M} b_k\,x[n-k]$$

starting **at rest** (all earlier outputs zero), which is what makes it LTI. If every $a_k$ is zero, the output is a weighted sum of recent inputs, the impulse response is just $b_0, b_1, \ldots, b_M$, and the filter is **FIR** (finite impulse response). With feedback ($a_k \ne 0$) it's **IIR**.

The first-order recursion $y[n] = a\,y[n-1] + b\,x[n]$ has $h[n] = b\,a^n u[n]$, settles to $bC/(1 - a)$ for a constant input C, and is stable for |a| < 1. The second-order one in the play has $h[n] = r^n\sin((n+1)\theta)/\sin\theta$ when $a_1 = 2r\cos\theta$ and $a_2 = -r^2$: a decaying oscillation, stable for r < 1.

## Block diagrams

A difference equation is built from three blocks: a **delay** by one sample (drawn $z^{-1}$), a **gain**, and an **adder**. Direct form I uses a chain of delays on the input and another on the output, N + M of them; direct form II shares one chain and needs only max(N, M). Each delay is one number of memory.

## Differential equations

Continuous-time LTI systems come from linear differential equations with constant coefficients. An RC low-pass obeys $\tau y' + y = x$, with $h(t) = \frac{1}{\tau}e^{-t/\tau}u(t)$; a series RLC is second order. The order is the number of energy-storing elements, as the order of a difference equation is the number of delays it needs.
:::

::: example
## Run $y[n] = 0.5\,y[n-1] + x[n]$ on a step, x[n] = 1 for n ≥ 0.

::: solution
1. **Run the recursion.**
   $$\begin{aligned} y[0] &= 1 \\ y[1] &= 1.5 \\ y[2] &= 1.75 \\ y[3] &= 1.875 \\ y[4] &= 1.9375 \end{aligned}$$
2. **Final value.** It closes on
   $$1/(1 - 0.5) = 2$$
3. **The pattern.** Each step halves the remaining distance to 2.

**Answer:** y = 1, 1.5, 1.75, 1.875, 1.9375, and so on, closing on 2.
:::
:::

::: example
## Describe $y[n] = 0.9\,y[n-1] - 0.81\,y[n-2] + x[n]$.

::: solution
1. **Radius.**
   $$r^2 = 0.81 \quad\text{so}\quad r = 0.9$$
2. **Angle.**
   $$\begin{aligned} 2r\cos\theta &= 0.9 \\ \cos\theta &= 0.5 \\ \theta &= \pi/3 \end{aligned}$$
3. **What it does.** So it rings with a period of 6 samples and loses 10% of its size each sample:
   $$\begin{aligned} h &= 1,\ 0.9,\ 0,\ -0.729, \\ &\quad -0.656,\ 0,\ 0.531, \ldots \end{aligned}$$
4. **Structure.** It needs two delays in direct form II.

**Answer:** it rings with a period of 6 samples (θ = π/3) and loses 10% of its size each sample (r = 0.9); it needs two delays in direct form II.
:::
:::

::: example
## Is $y[n] = 1.5\,y[n-1] - 0.5\,y[n-2] + x[n]$ stable?

::: solution
1. **Find the roots.**
   $$z^2 - 1.5z + 0.5 = 0$$
   gives z = 1 and z = 0.5.
2. **Fit the impulse response.** Fitting $h[n] = A + B(0.5)^n$ to h[0] = 1 and h[1] = 1.5 gives A = 2 and B = −1:
   $$h[n] = 2 - 0.5^n$$
3. **Test stability.** It stays bounded, but it doesn't die away, so $\sum\lvert h\rvert$ diverges and the system isn't BIBO stable: a step input makes the output grow like a ramp.
4. **The boundary case.** A pole exactly on the unit circle is the boundary case.

**Answer:** no, it isn't BIBO stable: $h[n] = 2 - 0.5^n$ doesn't die away, because of the pole at z = 1 on the unit circle.
:::
:::

::: check
given: a = 0.5 | 0.8 | -0.7; b = 0.5 | 2; k = 2 | 3 | 5
answer: b*a^k
tolerance: 0.5%

## $y[n] = \given{a}\,y[n-1] + \given{b}\,x[n]$ starts at rest and gets an impulse at n = 0. What is y[{k}]?

::: right
Right. $h[k] = ba^k$ = {answer}.
:::

::: misses
- [a^k] Include the input gain b.
- [b*a^(k - 1)] y[0] = b already; then k multiplications by a.
- [any] Not yet. $b\,a^k$.
:::
:::

::: check
given: a = 0.5 | 0.75 | 0.9; b = 2 | 0.5; C = 2 | 10
answer: b*C/(1 - a)
tolerance: 0.5%

## $y[n] = \given{a}\,y[n-1] + \given{b}\,x[n]$ is fed a constant {C} for a long time. What does y settle to?

::: right
Right. At steady state y = ay + bC: {answer}.
:::

::: misses
- [b*C] The feedback adds up past inputs: solve y = ay + bC.
- [C/(1 - a)] Include the input gain b.
- [any] Not yet. $bC/(1 - a)$.
:::
:::

::: check
given: a = 0.5 | 0.9; n = 2 | 3 | 4
answer: (1 - a^(n + 1))/(1 - a)
tolerance: 0.5%

## $y[n] = \given{a}\,y[n-1] + x[n]$ starts at rest and is fed a unit step. What is y[{n}]?

::: right
Right. $1 + a + \cdots + a^n$ = {answer}.
:::

::: misses
- [1/(1 - a)] That's where it ends up; at n = {n} it isn't there yet.
- [any] Not yet. Run the recursion, or add up h.
:::
:::

::: check
kind: parts
given: b0 = 1 | 0.5; b1 = 2 | -1; b2 = 1 | 0.25

## An FIR filter computes $y[n] = \given{b0}\,x[n] + \given{b1}\,x[n-1] + \given{b2}\,x[n-2]$.

::: part
answer: b1
tolerance: 0.5%

### What is h[1]?

::: misses
- [any] An FIR filter's impulse response is its coefficient list.
:::
:::

::: part
answer: b0 + b1 + b2
tolerance: 0.5%

### What does a constant input of 1 give at the output, once the filter is full?

::: misses
- [any] The sum of the coefficients.
:::
:::
:::

::: check
kind: parts
given: N = 2 | 4 | 6; M = 1 | 2 | 5

## A filter has feedback terms up to y[n − {N}] and input terms up to x[n − {M}].

::: part
answer: N + M
tolerance: 0

### How many delays does direct form I use?

::: misses
- [any] One chain of {N} on the output and one of {M} on the input.
:::
:::

::: part
answer: (N + M + abs(N - M))/2
tolerance: 0

### How many does direct form II use?

::: misses
- [N + M] Direct form II shares one chain of delays.
- [any] Not yet. max(N, M).
:::
:::
:::

::: check
given: r = 0.8 | 0.95; theta = 0.5 | 1; n = 3 | 6
answer: r^n*sin((n + 1)*theta)/sin(theta)
tolerance: 0.5%

## The recursion $y[n] = 2r\cos\theta\,y[n-1] - r^2y[n-2] + x[n]$ has r = {r} and θ = {theta}. What is h[{n}]?

::: right
Right. $r^n\sin((n+1)\theta)/\sin\theta$ = {answer}.
:::

::: misses
- [r^n*cos(n*theta)] The impulse response of this recursion has $\sin((n+1)\theta)/\sin\theta$, starting at 1.
- [any] Not yet. $r^n\sin((n+1)\theta)/\sin\theta$.
:::
:::

::: check
given: tau = 0.001 | 0.1 | 2; t = 1 | 3
answer: exp(-t)/tau
tolerance: 0.5%

## A system obeys $\tau y' + y = x$ with τ = {tau} s. What is its impulse response at t = {t}τ?

::: right
Right. $\frac{1}{\tau}e^{-t/\tau}$ at {t} time constants: {answer} per second.
:::

::: misses
- [exp(-t)] The impulse response has area 1, so its height scales as 1/τ.
- [any] Not yet. $h(t) = e^{-t/\tau}/\tau$.
:::
:::

::: check
kind: parts
given: a = 0.5 | 0.8 | 0.9 | -0.5

## $y[n] = \given{a}\,y[n-1] + x[n]$ is driven by a step from n = 0.

::: part
answer: 1 + a + a^2
tolerance: 0.5%

### What is y[2]?

::: misses
- [any] y[0] = 1, then each step multiplies by a and adds 1.
:::
:::

::: part
answer: 1/(1 - a)
tolerance: 0.5%

### Where does it settle?

::: misses
- [any] Set y[n] = y[n − 1].
:::
:::
:::

::: check
kind: parts
given: r = 0.8 | 0.9 | 0.95; N = 6 | 8 | 12

## A resonator $y[n] = a_1 y[n-1] + a_2 y[n-2] + x[n]$ should ring with a period of {N} samples and lose {r} of its size each sample (pole radius r = {r}).

::: part
answer: 2*r*cos(2*pi/N)
tolerance: 0.5%

### What is $a_1$?

::: misses
- [any] $2r\cos\theta$ with $\theta = 2\pi/N$.
:::
:::

::: part
answer: -r^2
tolerance: 0.5%

### What is $a_2$?

::: misses
- [r^2] The recursion subtracts $r^2 y[n-2]$.
- [any] $-r^2$.
:::
:::
:::

::: explain
## Why does a one-line recursion like $y[n] = 0.9\,y[n-1] + x[n]$ have an infinitely long impulse response, while an FIR filter's ends?

::: model
In the recursion, each output is computed from the previous output, so the impulse that entered at n = 0 is carried forward forever: y[1] holds 0.9 of it, y[2] holds 0.9 of that, and so on, shrinking but never reaching zero. An FIR filter only looks back at inputs, never at its own outputs, so once the impulse has passed through its last tap, M samples later, nothing remembers it and the output is exactly zero. Feedback is what lets a few coefficients produce a long, decaying response, which is why IIR filters are so cheap for a given sharpness.
:::
:::

::: practice

::: check
given: a = 0.6 | -0.5; k = 2 | 4
answer: a^k
tolerance: 0.5%

## Impulse response of $y[n] = \given{a}y[n-1] + x[n]$ at n = {k}?

::: misses
- [any] $a^k$.
:::
:::

::: check
given: a = 0.5 | 0.8; C = 1 | 4
answer: C/(1 - a)
tolerance: 0.5%

## Steady output of $y[n] = \given{a}y[n-1] + x[n]$ for a constant input {C}?

::: misses
- [any] $C/(1 - a)$.
:::
:::

::: check
given: N = 3 | 8; M = 2 | 4
answer: (N + M + abs(N - M))/2
tolerance: 0

## Delays in direct form II for feedback order {N} and feedforward order {M}?

::: misses
- [any] max(N, M).
:::
:::
:::
