Difference Equations
A linear constant-coefficient difference equation relates the current output to past outputs and inputs. It is how digital filters are implemented, how discrete-time models of populations, loans and control loops are written, and the discrete analogue of differential equations. The z-transform solves them the way the Laplace transform solves differential equations.
Solving by recursion
can always be computed forward from initial conditions. That is what a filter does sample by sample.
Solving in closed form
The homogeneous equation has solutions where : the characteristic roots , which are exactly the poles of . The general solution is , with fixed by the initial conditions.
import numpy as np
# y[n] = 1.5 y[n-1] - 0.56 y[n-2], with y[0] = 1, y[1] = 2
y = [1.0, 2.0]
for n in range(2, 12):
y.append(1.5 * y[-1] - 0.56 * y[-2])
roots = np.roots([1, -1.5, 0.56])
A = np.array([[1, 1], roots]) # y[0] = c1 + c2, y[1] = c1 r1 + c2 r2
c = np.linalg.solve(A, [1.0, 2.0])
closed = [c[0] * roots[0] ** n + c[1] * roots[1] ** n for n in range(12)]
print("roots:", np.round(roots, 4).tolist(), " c:", np.round(c, 4).tolist())
print("recursion: ", np.round(y, 5).tolist())
print("closed form:", np.round(closed, 5).tolist())roots: [0.8, 0.7] c: [13.0, -12.0]
recursion: [1.0, 2.0, 2.44, 2.54, 2.4436, 2.243, 1.99608, 1.73805, 1.48926, 1.26059, 1.05689, 0.87941]
closed form: [1.0, 2.0, 2.44, 2.54, 2.4436, 2.243, 1.99608, 1.73805, 1.48926, 1.26059, 1.05689, 0.87941]
Repeated roots give terms; complex roots give damped oscillations .
Fibonacci, a classic difference equation
has characteristic roots and , giving Binet's formula . As a filter, the root is a pole outside the unit circle: Fibonacci growth is an unstable system.
import numpy as np
phi, psi = (1 + np.sqrt(5)) / 2, (1 - np.sqrt(5)) / 2
fib = [0, 1]
for n in range(2, 31):
fib.append(fib[-1] + fib[-2])
binet = [round((phi ** n - psi ** n) / np.sqrt(5)) for n in range(31)]
print("F[30] by recursion:", fib[30], " by Binet:", binet[30], " all equal:", fib == binet)
print("ratio F[30]/F[29]:", fib[30] / fib[29], " phi:", phi)F[30] by recursion: 832040 by Binet: 832040 all equal: True
ratio F[30]/F[29]: 1.6180339887482036 phi: 1.618033988749895
Loans and savings
A savings account with monthly rate and deposit follows : a one-pole system with pole (growth). Its step response is the familiar annuity formula .
In software
scipy.signal.lfilter(b, a, x, zi=...)solves difference equations with initial conditions;lfilticconverts past values into the needed state.- Discrete-time state-space models (
scipy.signal.dlti, control libraries) are systems of first-order difference equations.
Exercises
Exercise 1: Characteristic roots
Find the general solution of .
Answer
.
Solution
has the double root 0.5, which brings the extra solution.
Exercise 2: Oscillation
Does oscillate? Does it decay?
Answer
It oscillates and decays.
Solution
has discriminant , so complex roots with , : a decaying oscillation.
Exercise 3: Annuity (code)
Deposit 100 per month at 0.5 % monthly interest for 120 months. Compare the recursion with the closed form.
Answer
Both give about 16387.93.
Solution
r, d = 0.005, 100.0
B = 0.0
for n in range(120):
B = (1 + r) * B + d
print(round(B, 2), round(d * ((1 + r) ** 120 - 1) / r, 2))16387.93 16387.93
Test yourself
The characteristic roots of a difference equation are…
- its zeros
- the poles of its transfer function
- its initial conditions
- always real
Complex characteristic roots with produce…
- growth
- a decaying oscillation
- a constant
- a step
Viewed as a filter, the Fibonacci recursion is…
- stable
- unstable, because one root exceeds 1
- an FIR filter
- a moving average