Until now, we have learned how to handle recurrence relations that are homogenous. In this exercise, we are going to take a look at recurrence relations that come in the form of:

an=c1an1+c2an2++ckank+f(n)a_n = c_1 a_{n-1} + c_2 a_{n-2} + \ldots + c_k a_{n-k} + f(n)

Equations of this form are called inhomogeneous linear recurrence relations because of the f(n) term. Yes, they are still linear relations. Let’s take this equation as an example:

an=3an1+n2a_n = 3a_{n-1} + n^2

With initial condition:

a0=1a_0 = 1

Here f(n) = n2.

The first step in deriving a closed-form solution is to temporarily ignore the f(n) term, treat the relation as homogeneous, and find its solution. Using techniques from previous exercises, we obtain the following homogeneous solution:

an(h)=α3na_{n}^{(h)} = \alpha 3^n

We use superscript h to denote this as the homogenous solution.

We now focus our attention on the f(n) term. Since f(n) is a polynomial of degree two, we can assume that an is proportional to n2 so:

an=An2a_n = An^2

However, before we proceed, we have to account for all powers of n less than two. Therefore our equation becomes:

an=An2+Bn+Ca_n = An^2 + Bn + C

To find the coefficients A, B, and C, we plug in the equation for an into the recurrence relation. We get:

An2+Bn+C=3(A(n1)2+B(n1)+C)+n2An^2 + Bn + C = 3(A(n-1)^2 + B(n-1) + C) + n^2

We can expand to get this:

An2+Bn+C=3A(n22n+1)+3B(n1)+3C+n2An^2 + Bn + C = 3A(n^2 - 2n + 1) + 3B(n-1) + 3C + n^2
An2+Bn+C=3An26An+3A+3Bn3B+3C+n2An^2 + Bn + C = 3An^2 - 6An + 3A + 3Bn - 3B + 3C + n^2

Grouping terms together:

An2+Bn+C=(3A+1)n2+(6A+3B)n+(3A3B+3C)An^2 + Bn + C = (3A+1)n^2 + (-6A+3B)n + (3A - 3B + 3C)
0=(2A+1)n2+(6A+2B)n+(3A3B+2C)0 = (2A+1)n^2 + (-6A+2B)n + (3A - 3B + 2C)

This equation must be valid for all n. Since A, B, and C are constants, this is only possible if all terms inside the parentheses equal zero.

This yields the following three equations with three unknowns:

2A+1=02A + 1 = 0
6A+2B=0-6A+2B = 0
3A3B+2C=03A - 3B + 2C = 0

We can solve this system of equations to get:

A=12A = -\frac{1}{2}
B=32B = -\frac{3}{2}
C=32C = -\frac{3}{2}

We now have what is called the particular solution, an(p):

an(p)=12n232n32a_{n}^{(p)} = -\frac{1}{2} n^2 - \frac{3}{2} n - \frac{3}{2}

The closed-form solution is the sum of the homogeneous solution and the particular solution:

an=an(h)+an(p)a_n = a_{n}^{(h)} + a_{n}^{(p)}
an=α3n12n232n32a_n = \alpha 3^n - \frac{1}{2} n^2 - \frac{3}{2} n - \frac{3}{2}

Plugging in the initial condition (a0 = 1) we get:

1=α321 = \alpha - \frac{3}{2}
α=52\alpha = \frac{5}{2}

The final closed-form solution is:

an=523n12n232n32a_n =\frac{5}{2}3^n - \frac{1}{2} n^2 - \frac{3}{2} n - \frac{3}{2}

This is the procedure for f(n) being a polynomial. If it were an exponential function or logarithmic, you start by assuming the particular solution is an(p) = Af(n) and continue to solve for the closed-form solution as we did before.

There exists a special case where f(n) is an exponential function of the form rn and its particular solution has a function that also exists in the homogenous solution; in this case, you would multiply by increasing powers of n just as you did for repeated roots to solve for the particular solution.



Derive the homogeneous solution for the following recurrence relation:

an=2an1+3(n1)a_n = 2a_{n-1} + 3(n-1)

Check the hint for the answer and hit Run when you’re ready to move on to the next checkpoint.


Derive the particular solution to the previous recurrence relation.

Check the hint for the answer and hit Run when you’re ready to move on to the next checkpoint.


Given the initial condition of a0 = 2 find the closed-form solution and implement it in the Python function closed_form().

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?