I tried to solve the following problem:
What is the generating function of the following recurrence relation? $$a_{n} = 3a_{n-1} + 1, 1\leqslant n, a_0= 2$$
First, I multiplied by $x^n$:
$$a_n x^n = 3a_{n-1} x^n + x^n$$
$$\sum^{\infty}_{n=0} a_n x^n= 3x \sum^{\infty}_{n=0} a_{n-1} x^{n-1} + \sum^{\infty}_{n=0} x^n$$
$$F(x) - a_0 = 3x F(x) + x \frac{1}{1-x}$$
$$F(x) - 2 = 3x F(x) + x \frac{1}{1-x}$$
$$F(x) -2 = \frac{3x}{1-x} + x \frac{1}{1-x}$$
$$F(x) = \frac{3x}{1-x} + x \frac{1}{1-x} + 2$$
I got stuck in here. I know how to solve a problem like this, when there isn't a plus constant number, so I tried to solve using that method:
$$a_n - 3 a_{n-1} = 1$$
$$a_1 = 3a_0 +1 = 7$$
$$a_2 = 3a_1 + 1 = 22$$
$A = 2 + 7x + 22x^2 + ...$
$3xA = 6x + 21x^2 + ...$
$(1-3x)A = 2 + x + x^2 + ... $
$A = 2 + \frac{x}{(1-3x)(1-x)}$
What should I do differently? And what is the correct solution of this problem?
I appreciate any kind of help.
Claude Leibovici's method is good, but if you want the generating function, you basically found it already. Just solve the equation, $$ F(x) - 2 = 3x F(x) + \frac{x}{1-x} \implies F(x) = \frac{2-x}{(1-x)(1-3x)} . $$ (At this point you instead seem to have substituted $F(x) = \frac{1}{1-x}$ in your working, which was wrong). Now, if we want to extract coefficients from this, I'd do partial fractions, $$ F(x) = \frac{5}{2(1-3x)} - \frac{1}{2(1-x)} . $$ From this, you should easily be able to find an expression for $a_n=[x^n]F(x)$ yourself.
EDIT: In your last equations, you also get the right answer except for an arithmetic error: $$ A = \frac{2}{1-3x} + \frac{x}{(1-3x)(1-x)}, $$ which is equal to $F(x)$ above.