# How to obtain a convergent solution iteratively for a linear system of equations

I am working on a problem that requires an iterative procedure to solve a linear system of equations, the system of equations in matrix form is:

$$\underbrace{\begin{bmatrix} r_{11} & r_{12} & r_{13} & \cdots & r_{1j} \\ r_{21} & r_{22} & r_{23} & \cdots & r_{2j} \\ r_{31} & r_{32} & r_{33} & \cdots & r_{3j} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ r_{i1} & r_{i2} & r_{i3} & \cdots & r_{ij} \end{bmatrix}}_\textit{R} \underbrace{\begin{bmatrix} a_{1} & 0 & 0 & \cdots & 0 \\ 0 & a_{2} & 0 & \cdots & 0 \\ 0 & 0 & a_{3} & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & a_{j} \end{bmatrix}}_\textit{A} + \underbrace{\begin{bmatrix} b_{1} & b_{2} & b_{3} & \cdots & b_{j} \\ b_{1} & b_{2} & b_{3} & \cdots & b_{j} \\ b_{1} & b_{2} & b_{3} & \cdots & b_{j} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ b_{1} & b_{2} & b_{3} & \cdots & b_{j} \end{bmatrix}}_\textit{B} = \underbrace{\begin{bmatrix} c_{1} & c_{1} & c_{1} & \cdots & c_{1} \\ c_{2} & c_{2} & c_{2} & \cdots & c_{2} \\ c_{3} & c_{3} & c_{3} & \cdots & c_{3} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ c_{i} & c_{i} & c_{i} & \cdots & c_{i} \end{bmatrix}}_\textit{C}\\ R_{i, j}A_{j, j} + B_{i, j} = C_{i, j}$$

Now matrix R is fully known (input), matrices A, B, C are unknown. I am working on an iterative procedure in which I provide initial guesses for A and B, then calculate C. The iteration is to be carried out till I get a converged value for C (output I require). So far no luck in choosing the initial guess too.

To illustrate the problem I have given an example for a $2 \times 2$ matrix:

$$\begin{bmatrix} r_{11} & r_{12} \\ r_{21} & r_{22} \\ \end{bmatrix} \begin{bmatrix} a_{1} & 0 \\ 0 & a_{2} \\ \end{bmatrix} + \begin{bmatrix} b_{1} & b_{2} \\ b_{1} & b_{2} \\ \end{bmatrix} = \begin{bmatrix} c_{1} & c_{1} \\ c_{2} & c_{2} \\ \end{bmatrix}\\$$ this gives us:

$$a_{1}r_{11} + b_{1} = a_{2}r_{12} + b_{2} = c_{1}\\ a_{1}r_{21} + b_{1} = a_{2}r_{22} + b_{2} = c_{2}$$

From these equations I get:

$$a_{2} = a_{1}\frac{(r_{11} - r_{21})}{(r_{12} - r_{22})}; \quad b_{2}=b_{1} + a_{1}\frac{(r_{11} - r_{21})(r_{11} - r_{12})}{(r_{12} - r_{22})}$$

This helps in the iterative method, but I would like to generalize it for a bigger matrix. Can this be solved with Mathematica?

UPDATE

I have added two reproducible Python codes that should be helpful for a $2 \times 2$ matrix:

### Code 1

import numpy as np
R = np.matrix([[2.5, 2.9], [2.3, 2.7]])
m = R.shape
n = R.shape
A = np.matrix(np.zeros(shape=(n,n)))
B = np.matrix(np.zeros(shape=(m,n)))
A[0,0] = 1
B[0,0] = 1
k = 0
for i in range (1, 10000):
A[0,0] = A[0, 0] - 0.0000001
B[0,0] = B[0, 0] - 0.0000001
for j in range(1, n):
A[j, j] = A[(j-1), (j-1)] * ((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1]))
B[0, j] = B[0, 0] + A[0, 0] * (((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1])) * (R[0, 0] - R[0, 1]))
B[1, 0] = B[0, 0]
B[1, j] = B[0, 0] + A[0, 0] * (((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1])) * (R[0, 0] - R[0, 1]))
C = R * A + B
C_convergence = np.all(np.all(np.diff(C, axis = 1) == 0, axis = 1), axis = 0)
k = k + 1
print k
print C


### Code 2

import numpy as np
R = np.matrix([[2.5, 2.9], [2.3, 2.7]])
m = R.shape
n = R.shape
A = np.matrix(np.zeros(shape=(n,n)))
B = np.matrix(np.zeros(shape=(m,n)))
A[0,0] = 1
B[0,0] = 1
k = 0
C_convergence = False
k = 0
while C_convergence == False:
A[0,0] = A[0, 0] - 0.0000001
B[0,0] = B[0, 0] - 0.0000001
for j in range(1, n):
A[j, j] = A[(j-1), (j-1)] * ((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1]))
B[0, j] = B[0, 0] + A[0, 0] * (((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1])) * (R[0, 0] - R[0, 1]))
B[1, 0] = B[0, 0]
B[1, j] = B[0, 0] + A[0, 0] * (((R[0, 0] - R[1, 0]) / (R[0, 1] - R[1, 1])) * (R[0, 0] - R[0, 1]))
C = R * A + B
C_convergence = np.all(np.all(np.diff(C, axis = 1) == 0, axis = 1), axis = 0)
k = k + 1
print k
print C

• In the 2x2 example you have 4 equations and 6 values to solve for {a1,a2,b1,b2,c1,c2}. Am I wrong? Feb 20, 2015 at 19:02
• Yes, but {a1, a2, b1, b2} are initial guesses and I shall iterate it till I get a converged {c1, c2}. Feb 20, 2015 at 19:14
• Could you please exemplify your iteration process with a numerical example in the 2x2 case? I just get an exact solution with 2 degrees of freedom in one step Feb 20, 2015 at 19:39
• I tried in Python which is provided in the update. Feb 20, 2015 at 20:20

r = RandomInteger[{-10, 10}, {2, 2}];