# Jacobi method

In numerical linear algebra, the Jacobi method is an iterative algorithm for determining the solutions of a strictly diagonally dominant system of linear equations. Each diagonal element is solved for, and an approximate value is plugged in. The process is then iterated until it converges. This algorithm is a stripped-down version of the Jacobi transformation method of matrix diagonalization. The method is named after Carl Gustav Jacob Jacobi.

## Description

Let

${\displaystyle A\mathbf {x} =\mathbf {b} }$

be a square system of n linear equations, where:

${\displaystyle A={\begin{bmatrix}a_{11}&a_{12}&\cdots &a_{1n}\\a_{21}&a_{22}&\cdots &a_{2n}\\\vdots &\vdots &\ddots &\vdots \\a_{n1}&a_{n2}&\cdots &a_{nn}\end{bmatrix}},\qquad \mathbf {x} ={\begin{bmatrix}x_{1}\\x_{2}\\\vdots \\x_{n}\end{bmatrix}},\qquad \mathbf {b} ={\begin{bmatrix}b_{1}\\b_{2}\\\vdots \\b_{n}\end{bmatrix}}.}$

Then A can be decomposed into a diagonal component D, a lower triangular part L and an upper triangular part U:

${\displaystyle A=D+L+U\qquad {\text{where}}\qquad D={\begin{bmatrix}a_{11}&0&\cdots &0\\0&a_{22}&\cdots &0\\\vdots &\vdots &\ddots &\vdots \\0&0&\cdots &a_{nn}\end{bmatrix}}{\text{ and }}L+U={\begin{bmatrix}0&a_{12}&\cdots &a_{1n}\\a_{21}&0&\cdots &a_{2n}\\\vdots &\vdots &\ddots &\vdots \\a_{n1}&a_{n2}&\cdots &0\end{bmatrix}}.}$

The solution is then obtained iteratively via

${\displaystyle \mathbf {x} ^{(k+1)}=D^{-1}(\mathbf {b} -(L+U)\mathbf {x} ^{(k)}),}$

where ${\displaystyle \mathbf {x} ^{(k)}}$ is the kth approximation or iteration of ${\displaystyle \mathbf {x} }$ and ${\displaystyle \mathbf {x} ^{(k+1)}}$ is the next or k + 1 iteration of ${\displaystyle \mathbf {x} }$. The element-based formula is thus:

${\displaystyle x_{i}^{(k+1)}={\frac {1}{a_{ii}}}\left(b_{i}-\sum _{j\neq i}a_{ij}x_{j}^{(k)}\right),\quad i=1,2,\ldots ,n.}$

The computation of ${\displaystyle x_{i}^{(k+1)}}$ requires each element in x(k) except itself. Unlike the Gauss–Seidel method, we can't overwrite ${\displaystyle x_{i}^{(k)}}$ with ${\displaystyle x_{i}^{(k+1)}}$, as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size n.

## Algorithm

Input: initial guess ${\displaystyle x^{(0)}}$ to the solution, (diagonal dominant) matrix ${\displaystyle A}$, right-hand side vector ${\displaystyle b}$, convergence criterion
Output: solution when convergence is reached
Comments: pseudocode based on the element-based formula above

${\displaystyle k=0}$
while convergence not reached do
for i := 1 step until n do
${\displaystyle \sigma =0}$
for j := 1 step until n do
if j ≠ i then
${\displaystyle \sigma =\sigma +a_{ij}x_{j}^{(k)}}$
end
end
${\displaystyle x_{i}^{(k+1)}={{\frac {1}{a_{ii}}}\left({b_{i}-\sigma }\right)}}$
end
${\displaystyle k=k+1}$
end

## Convergence

The standard convergence condition (for any iterative method) is when the spectral radius of the iteration matrix is less than 1:

${\displaystyle \rho (D^{-1}(L+U))<1.}$

A sufficient (but not necessary) condition for the method to converge is that the matrix A is strictly or irreducibly diagonally dominant. Strict row diagonal dominance means that for each row, the absolute value of the diagonal term is greater than the sum of absolute values of other terms:

${\displaystyle \left|a_{ii}\right|>\sum _{j\neq i}{\left|a_{ij}\right|}.}$

The Jacobi method sometimes converges even if these conditions are not satisfied.

Note that the Jacobi method does not converge for every symmetric positive-definite matrix. For example

${\displaystyle A={\begin{pmatrix}29&2&1\\2&6&1\\1&1&{\frac {1}{5}}\end{pmatrix}}\quad \Rightarrow \quad D^{-1}(L+U)={\begin{pmatrix}0&{\frac {2}{29}}&{\frac {1}{29}}\\{\frac {1}{3}}&0&{\frac {1}{6}}\\5&5&0\end{pmatrix}}\quad \Rightarrow \quad \rho (D^{-1}(L+U))\approx 1.0661\,.}$

## Example

A linear system of the form ${\displaystyle Ax=b}$ with initial estimate ${\displaystyle x^{(0)}}$ is given by

${\displaystyle A={\begin{bmatrix}2&1\\5&7\\\end{bmatrix}},\ b={\begin{bmatrix}11\\13\\\end{bmatrix}}\quad {\text{and}}\quad x^{(0)}={\begin{bmatrix}1\\1\\\end{bmatrix}}.}$

We use the equation ${\displaystyle x^{(k+1)}=D^{-1}(b-(L+U)x^{(k)})}$, described above, to estimate ${\displaystyle x}$. First, we rewrite the equation in a more convenient form ${\displaystyle D^{-1}(b-(L+U)x^{(k)})=Tx^{(k)}+C}$, where ${\displaystyle T=-D^{-1}(L+U)}$ and ${\displaystyle C=D^{-1}b}$. From the known values

${\displaystyle D^{-1}={\begin{bmatrix}1/2&0\\0&1/7\\\end{bmatrix}},\ L={\begin{bmatrix}0&0\\5&0\\\end{bmatrix}}\quad {\text{and}}\quad U={\begin{bmatrix}0&1\\0&0\\\end{bmatrix}}.}$

we determine ${\displaystyle T=-D^{-1}(L+U)}$ as

${\displaystyle T={\begin{bmatrix}1/2&0\\0&1/7\\\end{bmatrix}}\left\{{\begin{bmatrix}0&0\\-5&0\\\end{bmatrix}}+{\begin{bmatrix}0&-1\\0&0\\\end{bmatrix}}\right\}={\begin{bmatrix}0&-1/2\\-5/7&0\\\end{bmatrix}}.}$

Further, ${\displaystyle C}$ is found as

${\displaystyle C={\begin{bmatrix}1/2&0\\0&1/7\\\end{bmatrix}}{\begin{bmatrix}11\\13\\\end{bmatrix}}={\begin{bmatrix}11/2\\13/7\\\end{bmatrix}}.}$

With ${\displaystyle T}$ and ${\displaystyle C}$ calculated, we estimate ${\displaystyle x}$ as ${\displaystyle x^{(1)}=Tx^{(0)}+C}$:

${\displaystyle x^{(1)}={\begin{bmatrix}0&-1/2\\-5/7&0\\\end{bmatrix}}{\begin{bmatrix}1\\1\\\end{bmatrix}}+{\begin{bmatrix}11/2\\13/7\\\end{bmatrix}}={\begin{bmatrix}5.0\\8/7\\\end{bmatrix}}\approx {\begin{bmatrix}5\\1.143\\\end{bmatrix}}.}$

The next iteration yields

${\displaystyle x^{(2)}={\begin{bmatrix}0&-1/2\\-5/7&0\\\end{bmatrix}}{\begin{bmatrix}5.0\\8/7\\\end{bmatrix}}+{\begin{bmatrix}11/2\\13/7\\\end{bmatrix}}={\begin{bmatrix}69/14\\-12/7\\\end{bmatrix}}\approx {\begin{bmatrix}4.929\\-1.714\\\end{bmatrix}}.}$

This process is repeated until convergence (i.e., until ${\displaystyle \|Ax^{(n)}-b\|}$ is small). The solution after 25 iterations is

${\displaystyle x={\begin{bmatrix}7.111\\-3.222\end{bmatrix}}.}$

### Another example

Suppose we are given the following linear system:

{\displaystyle {\begin{aligned}10x_{1}-x_{2}+2x_{3}&=6,\\-x_{1}+11x_{2}-x_{3}+3x_{4}&=25,\\2x_{1}-x_{2}+10x_{3}-x_{4}&=-11,\\3x_{2}-x_{3}+8x_{4}&=15.\end{aligned}}}

If we choose (0, 0, 0, 0) as the initial approximation, then the first approximate solution is given by

{\displaystyle {\begin{aligned}x_{1}&=(6+0-(2*0))/10=0.6,\\x_{2}&=(25+0+0-(3*0))/11=25/11=2.2727,\\x_{3}&=(-11-(2*0)+0+0)/10=-1.1,\\x_{4}&=(15-(3*0)+0)/8=1.875.\end{aligned}}}

Using the approximations obtained, the iterative procedure is repeated until the desired accuracy has been reached. The following are the approximated solutions after five iterations.

${\displaystyle x_{1}}$ ${\displaystyle x_{2}}$ ${\displaystyle x_{3}}$ ${\displaystyle x_{4}}$
0.6 2.27272 -1.1 1.875
1.04727 1.7159 -0.80522 0.88522
0.93263 2.05330 -1.0493 1.13088
1.01519 1.95369 -0.9681 0.97384
0.98899 2.0114 -1.0102 1.02135

The exact solution of the system is (1, 2, −1, 1).

### An example using Python and Numpy

The following numerical procedure simply iterates to produce the solution vector.

def jacobi(A, b, x_init, epsilon=1e-10, max_iterations=500):
D = np.diag(np.diag(A))
LU = A - D
x = x_init
for i in range(max_iterations):
D_inv = np.diag(1 / np.diag(D))
x_new = np.dot(D_inv, b - np.dot(LU, x))
if np.linalg.norm(x_new - x) < epsilon:
return x_new
x = x_new
return x

# problem data
A = np.array([
[5, 2, 1, 1],
[2, 6, 2, 1],
[1, 2, 7, 1],
[1, 1, 2, 8]
])
b = np.array([29, 31, 26, 19])

# you can choose any starting vector
x_init = np.zeros(len(b))
x = jacobi(A, b, x_init)

print('x:', x)
print('computed b:', np.dot(A, x))
print('real b:', b)

Produces the output:

x: [3.99275362 2.95410628 2.16183575 0.96618357]
computed b: [29. 31. 26. 19.]
real b: [29 31 26 19]

## Weighted Jacobi method

The weighted Jacobi iteration uses a parameter ${\displaystyle \omega }$ to compute the iteration as

${\displaystyle \mathbf {x} ^{(k+1)}=\omega D^{-1}(\mathbf {b} -(L+U)\mathbf {x} ^{(k)})+\left(1-\omega \right)\mathbf {x} ^{(k)}}$

with ${\displaystyle \omega =2/3}$ being the usual choice.[1]

### Convergence in the symmetric positive definite case

In case that the system matrix ${\displaystyle A}$ is of symmetric positive-definite type one can show convergence.

Let ${\displaystyle C=C_{\omega }=I-\omega D^{-1}A}$ be the iteration matrix. Then, convergence is guaranteed for

${\displaystyle \rho (C_{\omega })<1\quad \Longleftrightarrow \quad 0<\omega <{\frac {2}{\lambda _{\text{max}}(D^{-1}A)}}\,,}$

where ${\displaystyle \lambda _{\text{max}}}$ is the maximal eigenvalue.

The spectral radius can be minimized for a particular choice of ${\displaystyle \omega =\omega _{\text{opt}}}$ as follows

${\displaystyle \min _{\omega }\rho (C_{\omega })=\rho (C_{\omega _{\text{opt}}})=1-{\frac {2}{\kappa (D^{-1}A)+1}}\quad {\text{for}}\quad \omega _{\text{opt}}:={\frac {2}{\lambda _{\text{min}}(D^{-1}A)+\lambda _{\text{max}}(D^{-1}A)}}\,,}$

where ${\displaystyle \kappa }$ is the matrix condition number.