# Matrix difference equation

How to use Mathematica's RSolve command to solve the difference equation in matrix form？

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


For example, the above formula is an example of mathematica. I want to rewrite a[n+1] and a[n] into matrix form and solve it. How do I write it?

Thank you for your answer and tell what I really need. I want to use Mathematica's RSolve command to solve the difference equation:

Error[n + 1]==S.Error[n]+1/2*stepsize*stepsize*A.A.minX1


Error,A and are all matrix, where A is a matrix in symbolic form. Here is my code. How can I write it correctly to run correctly？

{r = 22, l = 2 10^-1, c = 1 10^-4, vi = 24, initvalueilerror = 0,
initvaluevcerror = 0, tstart = 0, tend = 0.08, stepsize = 0.00006};
n = (tend - tstart)/stepsize;
A = {{0, -1/l}, {1/c, -1/(r c)}};
B = {1/l, 0};
G = Inverse[A];
S = Inverse[DiagonalMatrix[{1, 1}] - stepsize*A];
minX1 = Transpose[{{0, 0}}];
Error = Transpose[{{ilerror, vcerror}}];
Error = Table[{initvalueilerror, initvaluevcerror}, {i, 0, n}];
ilerror = Table[0, {i, 0, n}];
vcerror = Table[0, {i, 0, n}];
sol = RSolve[
Error[n + 1] == S . Error[n] + 1/2*stepsize*stepsize*A . A . minX1,
Error[0] == minX1, Error[n], n];
Error /. sol[[1]] // MatrixForm;
Max[Abs[ilerror]]
Max[Abs[vcerror]]

• As far as I know, RSolve can not deal with matrices. However, you can solve the problem iteratively. Commented Feb 16, 2023 at 10:38
• It is not clear what Error is intended to be. The first definition of Error, i.e., Error = Transpose[{{ilerror, vcerror}}] is overwritten by the second definition, i.e., Error = Table[{initvalueilerror, initvaluevcerror}, {i, 0, n}] Then you try to use Error in the recurrence equation as the unknown matrix. Your code doesn't make a lot of sense (at least to me) and you haven't said what you are trying to do with the code. Commented Feb 17, 2023 at 4:01
• Error is a column vector containing ilerror and vcerror. Error=Table [{initvalueileerror, initvaluevcerror}, {i, 0, n}]. I want to define a table for it, and then store the calculated data in the table. initvalueileerror and initvaluevcerror are the values I want to initialize for ileerror and vcerror Commented Feb 17, 2023 at 4:27

\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global*"]

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

(* {a[n] -> -1 + 2^(1 + n)} *)


Converting result to matrices

a[dim_Integer?Positive, n_Integer?NonNegative] :=
(2^(n + 1) - 1)*IdentityMatrix[dim]

a[3, 5] // MatrixForm


And @@@ Table[
a[m, n + 1] - 2 a[m, n] == IdentityMatrix[m], {m, 2, 5}, {n, 0, 10}]

(* {True, True, True, True} *)


EDIT: As pointed out by J.M. in a comment, you can solve directly by specifying the matrix dimension:

Clear[a]

MatrixForm /@ (mat =
Table[RSolveValue[{a[n + 1] - 2 a[n] == IdentityMatrix[m],
a[0] == IdentityMatrix[m]}, a[n] \[Element]
Matrices[{m, m}], n], {m, 5}])


This agrees with the result of (2^(n + 1) - 1) * IdentityMatrix[m]

EDIT 2: More simply,

Assuming[a[n] ∈ Matrices[{m, m}, Reals] &&
m ∈ PositiveIntegers,
RSolveValue[{a[n + 1] - 2 a[n] == IdentityMatrix[m],
a[0] == IdentityMatrix[m]}, a[n], n]]

(* (-1 + 2^(1 + n)) IdentityMatrix[m] *)


Or, the m ∈ PositiveIntegers can be implied,

Assuming[a[n] ∈ Matrices[{m, m}, Reals],
RSolveValue[{a[n + 1] - 2 a[n] == IdentityMatrix[m],
a[0] == IdentityMatrix[m]}, a[n], n]]

(* (-1 + 2^(1 + n)) IdentityMatrix[m] *)

• There currently isn't a way to handle matrices of indeterminate dimensions directly, so this should also suffice: RSolve[{a[n + 1] - 2 a[n] == IdentityMatrix[3], a[0] == IdentityMatrix[3]}, a[n] ∈ Matrices[3], n]`. Commented Feb 16, 2023 at 12:18
• @J.M.'spersistentexhaustion - Thanks, see last edit. Commented Feb 16, 2023 at 18:46
• Note that it is not necessary to assume that the the components of the matrices are real. Commented Feb 16, 2023 at 20:44
• Thank you for your answer, but my real need is like this. Can you help me see if it can be solved? Commented Feb 17, 2023 at 2:40