TExcelTAD
Home › Fourier Analysis & Signals › Difference Equations

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

y[n]=1.5 y[n−1]−0.56 y[n−2]+x[n]y[n] = 1.5\,y[n-1] - 0.56\,y[n-2] + x[n] can always be computed forward from initial conditions. That is what a filter does sample by sample.

Solving in closed form

The homogeneous equation y[n]−1.5y[n−1]+0.56y[n−2]=0y[n] - 1.5y[n-1] + 0.56y[n-2] = 0 has solutions λn\lambda^n where λ2−1.5λ+0.56=0\lambda^2 - 1.5\lambda + 0.56 = 0: the characteristic roots λ=0.8,0.7\lambda = 0.8, 0.7, which are exactly the poles of H(z)H(z). The general solution is y[n]=c10.8n+c20.7ny[n] = c_1 0.8^n + c_2 0.7^n, with c1,c2c_1, c_2 fixed by the initial conditions.

python · try it yourself
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 nλnn\lambda^n terms; complex roots re±iθre^{\pm i\theta} give damped oscillations rncos⁡(θn+φ)r^n \cos(\theta n + \varphi).

Fibonacci, a classic difference equation

F[n]=F[n−1]+F[n−2]F[n] = F[n-1] + F[n-2] has characteristic roots φ=(1+5)/2\varphi = (1 + \sqrt{5})/2 and ψ=(1−5)/2\psi = (1 - \sqrt{5})/2, giving Binet's formula F[n]=(φn−ψn)/5F[n] = (\varphi^n - \psi^n)/\sqrt{5}. As a filter, the root φ>1\varphi > 1 is a pole outside the unit circle: Fibonacci growth is an unstable system.

python · try it yourself
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 rr and deposit dd follows B[n]=(1+r)B[n−1]+dB[n] = (1 + r)B[n-1] + d: a one-pole system with pole 1+r>11 + r > 1 (growth). Its step response is the familiar annuity formula B[n]=d(1+r)n−1rB[n] = d\frac{(1+r)^n - 1}{r}.

In software

  • scipy.signal.lfilter(b, a, x, zi=...) solves difference equations with initial conditions; lfiltic converts 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 y[n]=y[n−1]−0.25 y[n−2]y[n] = y[n-1] - 0.25\,y[n-2].

Answer

y[n]=(c1+c2n) 0.5ny[n] = (c_1 + c_2 n)\, 0.5^n.

Solution

λ2−λ+0.25=(λ−0.5)2\lambda^2 - \lambda + 0.25 = (\lambda - 0.5)^2 has the double root 0.5, which brings the extra n⋅0.5nn \cdot 0.5^n solution.

Exercise 2: Oscillation

Does y[n]=1.6 y[n−1]−0.8 y[n−2]y[n] = 1.6\,y[n-1] - 0.8\,y[n-2] oscillate? Does it decay?

Answer

It oscillates and decays.

Solution

λ2−1.6λ+0.8=0\lambda^2 - 1.6\lambda + 0.8 = 0 has discriminant 2.56−3.2<02.56 - 3.2 < 0, so complex roots with ∣λ∣2=0.8\lvert \lambda \rvert^2 = 0.8, ∣λ∣≈0.894<1\lvert \lambda \rvert \approx 0.894 < 1: 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
python · try it yourself
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…

  1. its zeros
  2. the poles of its transfer function
  3. its initial conditions
  4. always real

Complex characteristic roots with ∣λ∣<1|\lambda| < 1 produce…

  1. growth
  2. a decaying oscillation
  3. a constant
  4. a step

Viewed as a filter, the Fibonacci recursion is…

  1. stable
  2. unstable, because one root exceeds 1
  3. an FIR filter
  4. a moving average