9.6: Using generating functions to solve recurrences
The approach we have seen thus far in this chapter is not the only way to solve recurrence equations. Additionally, it really only applies to linear recurrence equations with constant coefficients. In the remainder of the chapter, we will look at some examples of how generating functions can be used as another tool for solving recurrence equations. In this section, our focus will be on linear recurrence equations. In Section 9.7, we will see how generating functions can solve a nonlinear recurrence.
Our first example is the homogeneous recurrence that corresponds to the advancement operator equation in Example 9.9 .
Consider the recurrence equation \(r_n+r_{n−1}−6r_{n−2}=0\) for the sequence \(\{r_n:n \geq 0\}\) with \(r_0=1\) and \(r_1=3\). This sequence has generating function
\(f(x) = \displaystyle \sum_{n=0}^{\infty} r_nx^n = r_0 + r_1x + r_2x^2 + r_3x^3 + \cdot \cdot \cdot\).
Now consider for a moment what the function \(xf(x)\) looks like. It has \(r_{n−1}\) as the coefficient on \(x_n\). Similarly, in the function \(−6x^2f(x)\), the coefficient on \(x^n\) is \(−6r_{n−2}\).
What is our point in all of this? Well, if we add them all up, notice what happens. The coefficient on \(x_n\) becomes \(r_n+r_{n−1}−6r_{n−2}\), which is 0 because of the recurrence equation! Now let's see how this all lines up:
\(f(x) = r_0 + r_1x + r_2x^2 + r_3x^3 + \cdot \cdot \cdot + r_nx^n + \cdot \cdot \cdot\)
\(xf(x) = 0 + r_0x + r_1x^2 + r_2x^3 + \cdot \cdot \cdot r_{n-1}x^n + \cdot \cdot \cdot\)
\(-6x^2f(x) = 0 + 0 - 6r_0x^2 - 6r_1x^3 + \cdot \cdot \cdot -6r_{n-2}x^n + \cdot \cdot \cdot\)
When we add the left-hand side, we get \(f(x)(1+x−6x^2)\). On the right-hand side, the coefficient on \(x^n\) for \(n \geq 2\) is 0 because of the recurrence equation. However, we are left with \(r_0+(r_0+r_1)x=1+4x\), using the initial conditions. Thus, we have the equation
\(f(x)(1+x−6x^2)=1+4x\),
or \(f(x)=(1+4x)/(1+x−6x^2)\). This is a generating function that we can attack using partial fractions in SageMath:
//Code
This shows us that
\(f(x) = \dfrac{6}{5} \dfrac{1}{1-2x} - \dfrac{1}{5} \dfrac{1}{1+3x} = \dfrac{6}{5} \displaystyle \sum_{n=0}^{\infty} 2^nx^n - \dfrac{1}{5} \sum_{n=0}^{\infty}(-3)^nx^n\).
From here, we read off \(r_n\) as the coefficient on \(x^n\) and have \(r_n = (6/5)2^n - (1/5)(-3)^n\).
Although there's a bit more work involved, this method can be used to solve nonhomogeneous recurrence equations as well, as the next example illustrates.
The recurrence equation \(r_n−r_{n−1}−2r_{n−2}=2^n\) is nonhomogeneous. Let \(r_0=2\) and \(r_1=1\). This time, to solve the recurrence, we start by multiplying both sides by \(x^n\). This gives the equation
\(r_nx^n - r_{n-1}x^n - 2r_{n-2}x^n = 2^nx^n\).
If we sum this over all values of \(n \geq 2\), we have
\(\displaystyle \sum_{n=2}^{\infty} r_nx^n - \sum_{n=2}^{\infty} r_{n-1}x^n - 2\sum_{n=2}^{\infty} r_{n-2}x^n = \sum_{n=2}^{\infty} 2^nx^n\).
The right-hand side you should readily recognize as being almost equal to \(1/(1−2x)\). We are missing the 1 and \(2x\) terms, however, so must subtract them from the rational function form of the series. On the left-hand side, however, we need to do a bit more work.
The first sum is just missing the first two terms of the series, so we can replace it by \(R(x)−(2+x)\), where \(R(x)=\sum_{n=0}^{\infty} r_nx^n\). The second sum is almost \(xR(x)\), except it's missing the first term. Thus, it's equal to \(xR(x)−2x\). The sum in the final term is simply \(x^2R(x)\). Thus, the equation can be rewritten as
\(R(x) - (2+x) - (xR(x) -2x) - 2x^2R(x) = \dfrac{1}{1-2x} - 1 -2x\).
A little bit of algebra then gets us to the generating function
\(R(x) = \dfrac{6x^2 - 5x + 2}{(1-2x)(1-x-2x^2)}\).
This generating function can be expanded using partial fractions in SageMath:
//Code
Therefore, using Example 8.7 for the second rational function, we have
\(R(x) = - \dfrac{1}{9(1-2x)} + \dfrac{2}{3(1-2x)^2} + \dfrac{13}{9(1+x)}\)
\(= - \dfrac{1}{9} \displaystyle \sum_{n=0}^{\infty} 2^nx^n + \dfrac{2}{3} \sum_{n=0}^{\infty} \dbinom{n+2-1}{1} 2^nx^n + \dfrac{13}{9} \sum_{n=0}^{\infty} (-1)^nx^n\).
From this generating function, we can read off that
\(r_n = - \dfrac{1}{9} 2^n + \dfrac{2(n+1)}{3} 2^n + \dfrac{13}{9} (-1)^n = \dfrac{5}{9} 2^n + \dfrac{2}{3} n2^n + \dfrac{13}{9} (-1)^n\).
The recurrence equations of the two examples in this section can both be solved using the techniques we studied earlier in the chapter. One potential benefit to the generating function approach for nonhomogeneous equations is that it does not require determining an appropriate form for the particular solution. However, the method of generating functions often requires that the resulting generating function be expanded using partial fractions. Both approaches have positives and negatives, so unless instructed to use a specific method, you should choose whichever seems most appropriate for a given situation. In the next section, we will see a recurrence equation that is most easily solved using generating functions because it is nonlinear.