I'm preparing to an exam and trying to solve $a_n=2 a_{n-1} - a_{n-2} + 2^n$, where $a_0=0$ and $a_1=1$.
This is my approach:
Let $A(z)=\sum_{n \geq 0} a_{n+2} z^{n+2}$, then:
$$\sum a_{n+2} z^{n+2} = 2 \sum a_{n+1} z^{n+2} - \sum a_n z^{n+2} + \sum (2z)^{n+2}$$
$$A(z)-a_0-a_1 z = 2z(A(z)-a_0)-z^2 A(z) + z^2 \frac{1}{1-2z}$$
$$A(z)(1-2z+z^2)=z+\frac{z^2}{1-2z}$$
$$A(z)=\frac{z(1-z)}{(1-2z)(1-2z+z^2)}=\frac{z}{(1-2z)(1-z)}=\frac{1}{1-2z}-\frac{1}{1-z}=\sum (\underbrace{2^n - 1}_{=a_n}) z^n$$
According to Wolfram, the result is different. Can you tell me where did I make the mistake?
You started off on the wrong foot by defining $A(z)=\sum_{n\ge 0}a_{n+2}z^{n+2}=\sum_{n\ge 2}a_nz^n$ and then, two lines later, calling this same quantity $A(z)-a_0-a_1z$: that implies that $A(z)=\sum_{n\ge 0}a_nz^n$, which is what you really wanted all along. The third line then would have been correct, except that you erred in evaluating the last term:
$$\sum_{n\ge 0}(2z)^{n+2}=\frac{(2z)^2}{1-2z}=\frac{4z^2}{1-2z}\;,$$
not $\frac{z^2}{1-2z}$. As a result,
$$A(z)=\frac{z(1+2z)}{(1-2z)(1-z)^2}=\frac4{1-2z}-\frac1{1-z}-\frac3{(1-z)^2}\;,$$
from which the correct result is easily obtained.
An alternative approach that I favor is to rewrite the recurrence so that it’s valid for all $n\ge 0$ on the extra assumption that $a_n=0$ for $n<0$. Here you get
$$a_n=2a_{n-1}-a_{n-2}+2^n-[n=0]-[n=1]\;,\tag{1}$$
where the last two terms are Iverson brackets chosen so that $(1)$ gives the correct values for $a_0$ and $a_1$. Now multiply through by $z^n$ and sum over $n\ge 0$ to get
$$\begin{align*} A(z)&=2zA(z)-z^2A(z)+\sum_{n\ge 0}(2z)^n-1-z\\ &=2zA(z)-z^2A(z)+\frac1{1-2z}-\frac{(1+z)(1-2z)}{1-2z}\\ &=2zA(z)-z^2A(z)+\frac{z(1+2z)}{1-2z} \end{align*}$$
and hence
$$A(z)=\frac{z(1+2z)}{(1-2z)(1-z)^2}\;.$$
From here the solution proceeds as before. I like this approach because once I get $(1)$ set up properly, everything is automatic: I don’t have to think much about the indices at all.