# Solving a Variable Number of Difference Equations with RSolve

I'm trying to solve a variable number of linear equations defined by

\begin{align} x_0 &= A+Bx_1 \\ x_i &= C+\frac{B}{2}(x_{i-1}+x_{i+1}), \hspace{.25cm} 1\leq i \leq n-1 \\ x_n &= C+Bx_{n-1} \\y_0 &= p +Fx_0 +Gy_1 \\ y_1 &= Fx_1+ \frac{G}{2}(y_0+y_2-p) \\ y_i &= F x_i + \frac{G}{2} \left( y_{i-1}+y_{i+1} \right), \hspace{.25cm} 2 \leq i \leq n-1 \\ y_n &= Fx_n +Gy_{n-1} \end{align} When I evaluate the code

Eqns = {x[0, A, B , C, p, F, G] == A + B*x[1, A, B , C, p, F, G],
x[i, A, B , C, p, F, G] ==  C + B/2*(x[i - 1, A, B , C, p, F, G] + x[i + 1, A, B , C, p,
F, G]),
x[n, A, B , C, p, F, G] == C + B*x[n - 1, A, B , C, p, F, G],
y[0, A, B , C, p, F, G] ==  p + F*x[0, A, B , C, p, F, G] + G*y[1, A, B , C, p, F, G],
y[1, A, B , C, p, F, G] == F*x[0, A, B , C, p, F, G] + G/2*(y[0, A, B , C, p,
F,G]+y[2,A,B,C,p,F,G] - p),
y[i, A, B , C, p, F, G] == F*x[i, A, B , C, p, F, G] + G/2*(y[i + 1, A, B , C, p, F, G]
+y[i - 1, A, B , C, p, F, G]),
y[n, A, B , C, p, F, G] == F*x[n, A, B , C, p, F, G] + G*y[n - 1, A, B , C, p, F, G]}

RSolveValue[Eqns, {x[i, A, B, C, p, F, G], y[i, A, B, C, p, F, G]}, i,Assumptions -> n >2 ]


I receive the error "For some branches of the general solution, the given boundary conditions lead to an empty solution". I've manually tried using Solve for $$n = 3, 4$$ and receive a non-trivial solution. Is it possible that there just isn't a closed form solution for arbitrary $$n$$? Or do I need to specify more assumptions? Appreciate any help!

It is more convenient, and also more consistent with standard usage, to write the equations in the question as

eqns = {x[0] == a + b*x[1],
x[i] == c + b/2*(x[i - 1] + x[i + 1]),
x[n] == c + b*x[n - 1],
y[0] == p + f*x[0] + g*y[1],
y[1] == f*x[0] + g/2*(y[0] + y[2] - p),
y[i] == f*x[i] + g/2*(y[i + 1] + y[i - 1]),
y[n] == f*x[n] + g*y[n - 1]}


(Note that Eqns[[5]] in the question is not consistent with the text version of the corresponding equation, but this discrepancy is not the source of the error message cited.)

The first three equations can be solved without difficulty.

sx = RSolve[eqns[[;; 3]], x[i], i] // FullSimplify // Flatten
(* {x[i] -> (2^-i (2^n (Sqrt[-1 + 1/b^2] + 1/b)^n (-2 Sqrt[-1 + 1/b^2] +
2/b)^i (-1 + b) (a - c) + 2^i (Sqrt[-1 + 1/b^2] + 1/b)^i (-2 Sqrt[-1 +
1/b^2] + 2/b)^n (-1 + b) (a - c) - 2^(i + n) Sqrt[-1 + 1/b^2]
(Sqrt[-1 + 1/b^2] + 1/b)^n b c + 2^i Sqrt[-1 + 1/b^2] (-2 Sqrt[-1 + 1/b^2]
+ 2/b)^n b c))/((2^n (Sqrt[-1 + 1/b^2] + 1/b)^n - (-2 Sqrt[-1 + 1/b^2]
+ 2/b)^n) Sqrt[-1 + 1/b^2] (-1 + b) b)} *)


As far as I know, this usage of RSolve is not documented, although a similar question was addressed in 267528. However, attempting to solve the remaining equations fails, as noted in the question, even for n as small as 3.

RSolve[eqns[[4 ;;]] /. n -> 3, y[i], i] // Flatten


RSolve::bvnul: For some branches of the general solution, the given boundary conditions lead to an empty solution.

Clearly, these latter equations can be solved, however.

eqns[[4 ;;]] /. {i -> 2, n -> 3};
Solve[%, Array[y, 4, 0]] // Simplify // Flatten
(* {y[0] -> ((4 - 5 g^2 + g^4) p + f (4 + 4 g - 3 g^2 - 2 g^3)
x[0] + f g^2 (2 x[2] + g x[3]))/(4 - 5 g^2 + g^4),
y[1] -> (f (-((-4 - 2 g + 2 g^2 + g^3) x[0]) + g (2 x[2] +
g x[3])))/(4 - 5 g^2 + g^4),
y[2] -> (f (g^2 (x[0] - 2 x[2]) + 4 x[2] - g^3 x[3] +
2 g (x[0] + x[3])))/(4 - 5 g^2 + g^4),
y[3] -> (f (g^3 (x[0] - 2 x[2]) + 4 g x[2] + g^2 (2 x[0] -
3 x[3]) + 4 x[3]))/(4 - 5 g^2 + g^4)} *)


after which the corresponding values of x[i] determined above can be inserted. In fact, any value of n > 2 can be solved, provided the coefficient matrix of the y equations is not singular.

I am hesitant to suggest that the failure of RSolve[eqns[[4 ;;]], y[i], i] to produce a solution is a bug. because this usage is undocumented. The distinction between the x[i] equations, for which RSolve produces a result, and the y[i] equations, for which is does not, is that the former has two boundary conditions, and the latter has three.

Addendum: General solution for expn[[4;;]]

Upon further consideration, I believe that RSolve cannot handle eqns[[4;;]] as written, since it entails three boundary conditions but can satisfy only two, because eqns[[6]] is a second order difference equation. A work-around is to replace y[0] in eqns[[4 ;; 6]] by (for instance) y0, so that RSolve does not try to evaluate it. Then,

sy = RSolve[eqns[[4 ;; 6]] /. y[0] -> y0, y, i] // Flatten


gives the general solution in terms of y0, and

Simplify[eqns[[7]] /. %];
sy0 = Solve[%, y0] // Simplify // Flatten


gives y0 (i.e., y[0]). These general expressions are, unfortunately, too long to reproduce here. To compare this result for n -> 3 with the earlier explicit result, compute

Simplify[sy0 /. n -> 3]
Simplify[{y[2], y[3], y[4]} /. sy /. %]
`

which is identical to the earlier result.