Solving Linear Recurrence Relations
Solve recurrence relations using the characteristic equation method and verify with Python.
25 min read
Intermediate
Introduction
Many sequences are defined recursively - each term depends on previous terms. The Fibonacci sequence is a classic example. We'll learn to solve these recurrences systematically.
Learning Objectives:
- Solve linear recurrence relations
- Use the characteristic equation method
- Verify solutions with Python
Linear Recurrence Relations
A linear recurrence with constant coefficients:
with initial conditions .
Example: Fibonacci Sequence
Characteristic equation: Assume :
Roots: (golden ratio and )
General solution:
Using initial conditions: (Binet's formula)
python
import math
def fibonacci_recursive(n):
"""Compute Fibonacci using recurrence"""
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
def fibonacci_closed_form(n):
"""Binet's formula"""
phi = (1 + math.sqrt(5)) / 2
psi = (1 - math.sqrt(5)) / 2
return int((phi**n - psi**n) / math.sqrt(5))
print("Fibonacci sequence:")
print("\nRecursive vs Closed Form:")
for n in range(15):
rec = fibonacci_recursive(n)
closed = fibonacci_closed_form(n)
print(f" F_{n:<2} = {rec:>4} (closed form: {closed:>4}) โ" if rec == closed else f" F_{n} mismatch!")Key Takeaways
- Recurrence: Define sequence recursively
- Characteristic equation: Solve for roots
- General solution: Linear combination of roots
- Initial conditions: Determine constants
- Closed form: Direct formula for
Next Lesson: Combinatorial proofs and bijections!