I came across the following recurrence in a blackpenredpen video while learning about generating functions:
$$ a_n = 2 a_{n - 1} - 2^n $$
with $a_0 = 5$. Before attempting to solve it using generating functions I solved it by expanding out the first few terms:
$$ a_n = 2^2 a_{n - 2} - 2 \cdot 2^n $$ $$ a_n = 2^3 a_{n - 3} - 3 \cdot 2^n $$ $$ a_n = 2^4 a_{n - 4} - 4 \cdot 2^n $$
and after a few more terms we deduce $a_n = 5 \cdot 2^n - n 2^n = (5 - n) 2^n$ which we verify using induction. There was some luck in arriving at this form, since it involved some guesswork.
How can this recurrence be solved using generating functions? Of course other methods for solving the recurrence will also be appreciated :).
The recurrence can be solved using generating functions. We have the recurrence:
$$ a_n = 2 a_{n - 1} - 2^n $$
Summing all $a_n$ and converting to power series we get, according to the recurrence relation:
$$ \sum_{n = 1}^{\infty} a_n x^n = 2 \sum_{n = 0}^{\infty} a_n x^{n + 1} - \sum_{n = 1}^{\infty} 2^n x^n$$ $$ \sum_{n = 1}^{\infty} a_n x^n = 2 x \sum_{n = 0}^{\infty} a_n x^n - \sum_{n = 1}^{\infty} 2^n x^n$$
We now define $A(x) = \sum_{n = 0}^{\infty} a_n x^n$. Plugging into above we get:
$$ A(x) - a_0 = 2x A(x) - \sum_{n = 1}^{\infty} 2^n x^n$$
Now using the fact that $x$ is arbitrary and we are only interested in the coefficients of the series, which are the elements of the recurrence relation, we may choose $x$ such that $\sum_{n = 1}^{\infty} 2^n x^n$ converges, getting:
$$ A(x) - a_0 = 2x A(x) - \left( \frac{1}{1 - 2x} - 1 \right) $$
Rearranging we get:
$$ A(x) = \frac{a_0 - 2a_0 x - 2x}{\left( 1 - 2x \right)^2} = \frac{5 - 12x}{\left( 1 - 2x \right)^2} $$
Applying partial fraction decomposition to the above gives us:
$$ A(x) = \frac{6}{1 - 2x} - \frac{1}{\left(1 - 2x \right)^2}$$
Now we need to convert above to a power series to obtain the coefficients which are the elements of the recurrence. We have that:
$$ \frac{1}{1 - 2x} = \sum_{n = 0}^{\infty} 2^n x^n $$
Therefore:
$$ A(x) = 6\sum_{n = 0}^{\infty} 2^n x^n - \frac{1}{\left(1 - 2x \right)^2}$$
We can deduce the series for $\frac{1}{\left(1 - 2x \right)^2}$ by differentiating $\frac{1}{1 - 2x}$:
$$ \frac{2}{\left( 1 - 2x \right)^2} = \frac{d}{dx} \frac{1}{1 - 2x} = \sum_{n = 1}^{\infty} 2^n \cdot n x^{n - 1}$$
From which we conclude that:
$$ A(x) = 6\sum_{n = 0}^{\infty} 2^n x^n - \sum_{n = 1}^{\infty} 2^{n - 1} \cdot n x^{n - 1}$$
Or:
$$ A(x) = 6\sum_{n = 0}^{\infty} 2^n x^n - \sum_{n = 0}^{\infty} (n + 1) 2^n x^n$$ $$ A(x) = \sum_{n = 0}^{\infty} (5 - n) 2^n x^n $$
Which means $a_n = (5 - n)2^n$.