# RSolve with two boundary conditions

I want to solve a recurrence equation that has boundary conditions on both sides:

$$a[0]= 1$$

$$a[n]= 10$$

$$a[i] = a[i-1]-a[i+1]\quad \forall i \in \{1, \ldots, n-1\}$$

This should be a well defined system with n+1 equations and n+1 unknowns.

I tried:

RSolve[{a[n] == a[n - 1] - a[n + 1], a[0] == 1,a[n] == 10}, a[n], n]


But Mathematica says the system is overdetermined:

RSolve::overdet: There are fewer dependent variables than equations, so the system is overdetermined.

The issue is likely that I have not encoded that $$\forall i \in \{1, \ldots, n-1\}$$ we have a[i] == a[i - 1] - a[i + 1] and only for the very last i: $$i=n$$ we have a[n] == 10, right? How can I encode this in RSolve?

• why not the boring Solve after creating a Table with values? You can solve for all the a[i]
– bmf
Commented Apr 28, 2022 at 19:57

Your code uses n in two roles: one for the defining recursion relation, and the other for the boundary condition. This confuses Mathematica (and the casual human reader, to boot.) Use different symbols for each of these two roles and it works a treat, yielding a general solution that works for all boundary values of n.

RSolve[{a[i] == a[i - 1] - a[i + 1], a[0] == 1, a[n] == 10}, a[i], i];
Simplify[%, Assumptions -> Element[n, Integers]]

(* {{a[i] -> ( 2^-i (5 2^(1 + n) (-1 + Sqrt[5])^i - (-1 - Sqrt[5])^n (-1 + Sqrt[5])^i + (-1 - Sqrt[5])^i (-5 2^(1 + n) + (-1 + Sqrt[5])^n)))/
((-1 + Sqrt[5])^n + (-1)^(1 + n) (1 + Sqrt[5])^n)}} *)


Clear["Global*"]

{n, StringForm["", f[n, k]],
StringForm[" &&\n",
`