Jump to content

Jacobi method: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
An optimization to the python algorithm (it was calculating in each iteration D_inv)
mNo edit summary
 
(33 intermediate revisions by 18 users not shown)
Line 1: Line 1:
{{Short description|Iterative method used to solve a linear system of equations}}
{{Distinguish|Jacobi eigenvalue algorithm}}
{{Distinguish|Jacobi eigenvalue algorithm}}


In [[numerical linear algebra]], the '''Jacobi method''' is an iterative algorithm for determining the solutions of a [[Diagonally dominant matrix|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 eigenvalue algorithm|Jacobi transformation method of matrix diagonalization]]. The method is named after [[Carl Gustav Jacob Jacobi]].
In [[numerical linear algebra]], the '''Jacobi method''' (a.k.a. the '''Jacobi iteration method''') is an iterative algorithm for determining the solutions of a [[Diagonally dominant matrix|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 eigenvalue algorithm|Jacobi transformation method of matrix diagonalization]]. The method is named after [[Carl Gustav Jacob Jacobi]].


== Description ==
== Description ==
Let <math>A\mathbf x = \mathbf b</math> be a square system of ''n'' linear equations, where:<math display="block">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}.</math>
Let


When <math>A</math> and <math>\mathbf b</math> are known, and <math>\mathbf x</math> is unknown, we can use the Jacobi method to approximate <math>\mathbf x</math>. The vector <math>\mathbf x^{(0)}</math> denotes our initial guess for <math>\mathbf x</math> (often <math>\mathbf x^{(0)}_i=0</math> for <math>i=1,2,...,n</math>). We denote <math>\mathbf{x}^{(k)}</math> as the ''k-''th approximation or iteration of <math>\mathbf{x}</math>, and <math>\mathbf{x}^{(k+1)}</math> is the next (or ''k''+1) iteration of <math>\mathbf{x}</math>.
:<math>A\mathbf x = \mathbf b</math>


=== Matrix-based formula ===
be a square system of ''n'' linear equations, where:


<math>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}.</math>
Then ''A'' can be decomposed into a [[diagonal matrix|diagonal]] component ''D'', a lower triangular part ''L'' and an upper triangular part ''U'':<math display="block">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}. </math>The solution is then obtained iteratively via
:<math> \mathbf{x}^{(k+1)} = D^{-1} (\mathbf{b} - (L+U) \mathbf{x}^{(k)}). </math>


=== Element-based formula ===
Then ''A'' can be decomposed into a [[diagonal matrix|diagonal]] component ''D'', a lower triangular part ''L'' and an upper triangular part ''U'':


The element-based formula for each row <math>i</math> is thus:<math display="block"> x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i -\sum_{j\ne i}a_{ij}x^{(k)}_j\right),\quad i=1,2,\ldots,n. </math>The computation of <math>x_i^{(k+1)}</math> requires each element in <math>\mathbf{x}^{(k)}</math> except itself. Unlike the [[Gauss–Seidel method]], we can't overwrite <math>x_i^{(k)}</math> with <math>x_i^{(k+1)}</math>, as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size ''n''.
:<math>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}. </math>

The solution is then obtained iteratively via
:<math> \mathbf{x}^{(k+1)} = D^{-1} (\mathbf{b} - (L+U) \mathbf{x}^{(k)}), </math>

where <math>\mathbf{x}^{(k)}</math> is the ''k''th approximation or iteration of <math>\mathbf{x}</math> and <math>\mathbf{x}^{(k+1)}</math> is the next or ''k'' + 1 iteration of <math>\mathbf{x}</math>. The element-based formula is thus:

:<math> x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i -\sum_{j\ne i}a_{ij}x^{(k)}_j\right),\quad i=1,2,\ldots,n. </math>

The computation of <math>x_i^{(k+1)}</math> requires each element in '''x'''<sup>(''k'')</sup> except itself. Unlike the [[Gauss–Seidel method]], we can't overwrite <math>x_i^{(k)}</math> with <math>x_i^{(k+1)}</math>, as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size ''n''.


== Algorithm ==
== Algorithm ==
'''Input:''' {{nowrap|initial guess <math>x^{(0)}</math> to the solution}}, (diagonal dominant) matrix <math>A</math>, right-hand side vector <math>b</math>, convergence criterion
'''Input:''' {{nowrap|initial guess ''x''<sup>(0)</sup> to the solution}}, (diagonal dominant) matrix ''A'', right-hand side vector ''b'', convergence criterion
'''Output:''' {{nowrap|solution when convergence is reached}}
'''Output:''' {{nowrap|solution when convergence is reached}}
'''Comments:''' {{nowrap|pseudocode based on the element-based formula above}}
'''Comments:''' pseudocode based on the element-based formula above
{{nowrap|<math> k = 0 </math>}}
{{nowrap|1=''k'' = 0}}
'''while''' convergence not reached '''do'''
'''while''' convergence not reached '''do'''
'''for''' i := 1 '''step until''' n '''do'''
'''for''' ''i'' := 1 '''step until''' n '''do'''
{{nowrap|<math> \sigma = 0 </math>}}
{{nowrap|1=''σ'' = 0}}
'''for''' j := 1 '''step until''' n '''do'''
'''for''' ''j'' := 1 '''step until''' n '''do'''
'''if''' j &ne; i '''then'''
'''if''' ''j'' &ne; ''i'' '''then'''
{{nowrap|<math> \sigma = \sigma + a_{ij} x_j^{(k)} </math>}}
{{nowrap|1=''σ'' = ''σ'' + ''a''<sub>''ij''</sub> ''x''<sub>''j''</sub><sup>(''k'')</sup>}}
'''end'''
'''end'''
'''end'''
'''end'''
{{nowrap|<math> x_i^{(k+1)} = {{\frac{1}{a_{ii}} \left( {b_i - \sigma } \right)}} </math>}}
{{nowrap|1=''x''<sub>''i''</sub><sup>(''k''+1)</sup> = (''b''<sub>''i''</sub> ''σ'') / ''a''<sub>''ii''</sub>}}
'''end'''
'''end'''
increment ''k''
{{nowrap|<math>k = k + 1</math>}}
'''end'''
'''end'''


Line 57: Line 50:
The Jacobi method sometimes converges even if these conditions are not satisfied.
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]].
Note that the Jacobi method does not converge for every symmetric [[positive-definite matrix]]. For example,
<math display="block">
For example

:<math>
A =
A =
\begin{pmatrix}
\begin{pmatrix}
Line 78: Line 69:
</math>
</math>


==Examples==
== Examples ==


===Example 1===
=== Example question ===
A linear system of the form <math>Ax=b</math> with initial estimate <math>x^{(0)}</math> is given by
A linear system of the form <math>Ax=b</math> with initial estimate <math>x^{(0)}</math> is given by


Line 99: Line 90:
\end{bmatrix} .</math>
\end{bmatrix} .</math>
We use the equation <math> x^{(k+1)}=D^{-1}(b - (L+U)x^{(k)})</math>, described above, to estimate <math>x</math>. First, we rewrite the equation in a more convenient form <math>D^{-1}(b - (L+U)x^{(k)}) = Tx^{(k)} + C</math>, where <math>T=-D^{-1}(L+U)</math> and <math>C = D^{-1}b</math>. From the known values
We use the equation <math> x^{(k+1)}=D^{-1}(b - (L+U)x^{(k)})</math>, described above, to estimate <math>x</math>. First, we rewrite the equation in a more convenient form <math>D^{-1}(b - (L+U)x^{(k)}) = Tx^{(k)} + C</math>, where <math>T=-D^{-1}(L+U)</math> and <math>C = D^{-1}b</math>. From the known values
<math display=block> D^{-1}=

:<math> D^{-1}=
\begin{bmatrix}
\begin{bmatrix}
1/2 & 0 \\
1/2 & 0 \\
Line 116: Line 106:
\end{bmatrix} .</math>
\end{bmatrix} .</math>
we determine <math> T=-D^{-1}(L+U) </math> as
we determine <math> T=-D^{-1}(L+U) </math> as
:<math> T=
<math display=block> T=
\begin{bmatrix}
\begin{bmatrix}
1/2 & 0 \\
1/2 & 0 \\
Line 137: Line 127:
\end{bmatrix} .</math>
\end{bmatrix} .</math>
Further, <math>C</math> is found as
Further, <math>C</math> is found as
<math display=block> C =

:<math> C =
\begin{bmatrix}
\begin{bmatrix}
1/2 & 0 \\
1/2 & 0 \\
Line 153: Line 142:
\end{bmatrix}. </math>
\end{bmatrix}. </math>
With <math>T</math> and <math>C</math> calculated, we estimate <math>x</math> as <math> x^{(1)}= Tx^{(0)}+C </math>:
With <math>T</math> and <math>C</math> calculated, we estimate <math>x</math> as <math> x^{(1)}= Tx^{(0)}+C </math>:
:<math> x^{(1)}=
<math display=block> x^{(1)}=
\begin{bmatrix}
\begin{bmatrix}
0 & -1/2 \\
0 & -1/2 \\
Line 178: Line 167:
\end{bmatrix} .</math>
\end{bmatrix} .</math>
The next iteration yields
The next iteration yields
:<math> x^{(2)}=
<math display=block> x^{(2)}=
\begin{bmatrix}
\begin{bmatrix}
0 & -1/2 \\
0 & -1/2 \\
Line 210: Line 199:
.</math>
.</math>


===Example 2===
===Example question 2===


Suppose we are given the following linear system:
Suppose we are given the following linear system:
Line 224: Line 213:


If we choose {{math|(0,&nbsp;0,&nbsp;0,&nbsp;0)}} as the initial approximation, then the first approximate solution is given by
If we choose {{math|(0,&nbsp;0,&nbsp;0,&nbsp;0)}} as the initial approximation, then the first approximate solution is given by
<math display=block>

:<math>
\begin{align}
\begin{align}
x_1 & = (6 + 0 - (2 * 0)) / 10 = 0.6, \\
x_1 & = (6 + 0 - (2 * 0)) / 10 = 0.6, \\
Line 233: Line 221:
\end{align}
\end{align}
</math>
</math>

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.
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.


Line 270: Line 257:
The exact solution of the system is {{math|(1,&nbsp;2,&nbsp;&minus;1,&nbsp;1)}}.
The exact solution of the system is {{math|(1,&nbsp;2,&nbsp;&minus;1,&nbsp;1)}}.


===Example 3 using Python and NumPy ===
=== Python example ===
<syntaxhighlight lang="numpy" line="1">
The following numerical procedure simply iterates to produce the solution vector.
import numpy as np


ITERATION_LIMIT = 1000
<syntaxhighlight lang="python">
def jacobi(A, b, x_init, epsilon=1e-10, max_iterations=500):
D = np.diag(np.diag(A))
LU = A - D
x = x_init
D_inv = np.diag(1 / np.diag(D))
for i in range(max_iterations):
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


# initialize the matrix
# problem data
A = np.array([
A = np.array([[10., -1., 2., 0.],
[5, 2, 1, 1],
[-1., 11., -1., 3.],
[2, 6, 2, 1],
[2., -1., 10., -1.],
[1, 2, 7, 1],
[0.0, 3., -1., 8.]])
# initialize the RHS vector
[1, 1, 2, 8]
b = np.array([6., 25., -11., 15.])
])
b = np.array([29, 31, 26, 19])


# prints the system
# you can choose any starting vector
print("System:")
x_init = np.zeros(len(b))
for i in range(A.shape[0]):
x = jacobi(A, b, x_init)
row = [f"{A[i, j]}*x{j + 1}" for j in range(A.shape[1])]
print(f'{" + ".join(row)} = {b[i]}')
print()


x = np.zeros_like(b)
print("x:", x)
for it_count in range(ITERATION_LIMIT):
print("computed b:", np.dot(A, x))
if it_count != 0:
print("real b:", b)
print(f"Iteration {it_count}: {x}")
</syntaxhighlight>
x_new = np.zeros_like(x)


for i in range(A.shape[0]):
Produces the output:
s1 = np.dot(A[i, :i], x[:i])
s2 = np.dot(A[i, i + 1:], x[i + 1:])
x_new[i] = (b[i] - s1 - s2) / A[i, i]
if x_new[i] == x_new[i-1]:
break


if np.allclose(x, x_new, atol=1e-10, rtol=0.):
<pre>
break
x: [3.99275362 2.95410628 2.16183575 0.96618357]

computed b: [29. 31. 26. 19.]
x = x_new
real b: [29 31 26 19]

</pre>
print("Solution: ")
print(x)
error = np.dot(A, x) - b
print("Error:")
print(error)
</syntaxhighlight>


== Weighted Jacobi method ==
== Weighted Jacobi method ==
Line 323: Line 314:
=== Convergence in the symmetric positive definite case ===
=== Convergence in the symmetric positive definite case ===


In case that the system matrix <math> A </math> is of symmetric [[Positive-definite matrix|positive-definite]] type one can show convergence.
In case that the system matrix <math> A </math> is of symmetric [[Positive-definite matrix|positive-definite]] type one can show convergence.


Let <math> C=C_\omega = I-\omega D^{-1}A </math> be the iteration matrix.
Let <math> C=C_\omega = I-\omega D^{-1}A </math> be the iteration matrix.
Line 335: Line 326:


The spectral radius can be minimized for a particular choice of <math> \omega = \omega_\text{opt} </math> as follows
The spectral radius can be minimized for a particular choice of <math> \omega = \omega_\text{opt} </math> as follows
:<math>
<math display="block">
\min_\omega \rho (C_\omega) = \rho (C_{\omega_\text{opt}}) = 1-\frac{2}{\kappa(D^{-1}A)+1}
\min_\omega \rho (C_\omega) = \rho (C_{\omega_\text{opt}}) = 1-\frac{2}{\kappa(D^{-1}A)+1}
\quad \text{for} \quad
\quad \text{for} \quad
\omega_\text{opt} := \frac{2}{\lambda_\text{min}(D^{-1}A)+\lambda_\text{max}(D^{-1}A)} \,,
\omega_\text{opt} := \frac{2}{\lambda_\text{min}(D^{-1}A)+\lambda_\text{max}(D^{-1}A)} \,,
</math>
</math>
where <math> \kappa </math> is the [[Condition_number#Matrices|matrix condition number]].
where <math> \kappa </math> is the [[Condition number#Matrices|matrix condition number]].


==See also==
==See also==
Line 359: Line 350:


{{Numerical linear algebra}}
{{Numerical linear algebra}}
{{Authority control}}


[[Category:Numerical linear algebra]]
[[Category:Numerical linear algebra]]

Latest revision as of 13:26, 18 April 2024

In numerical linear algebra, the Jacobi method (a.k.a. the Jacobi iteration 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

[edit]

Let be a square system of n linear equations, where:

When and are known, and is unknown, we can use the Jacobi method to approximate . The vector denotes our initial guess for (often for ). We denote as the k-th approximation or iteration of , and is the next (or k+1) iteration of .

Matrix-based formula

[edit]

Then A can be decomposed into a diagonal component D, a lower triangular part L and an upper triangular part U:The solution is then obtained iteratively via

Element-based formula

[edit]

The element-based formula for each row is thus:The computation of requires each element in except itself. Unlike the Gauss–Seidel method, we can't overwrite with , as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size n.

Algorithm

[edit]
Input: initial guess x(0) to the solution, (diagonal dominant) matrix A, right-hand side vector b, convergence criterion
Output: solution when convergence is reached
Comments: pseudocode based on the element-based formula above

k = 0
while convergence not reached do
    for i := 1 step until n do
        σ = 0
        for j := 1 step until n do
            if ji then
                σ = σ + aij xj(k)
            end
        end
        xi(k+1) = (biσ) / aii
    end
    increment k
end

Convergence

[edit]

The standard convergence condition (for any iterative method) is when the spectral radius of the iteration matrix is less than 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:

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,

Examples

[edit]

Example question

[edit]

A linear system of the form with initial estimate is given by

We use the equation , described above, to estimate . First, we rewrite the equation in a more convenient form , where and . From the known values we determine as Further, is found as With and calculated, we estimate as : The next iteration yields This process is repeated until convergence (i.e., until is small). The solution after 25 iterations is

Example question 2

[edit]

Suppose we are given the following linear system:

If we choose (0, 0, 0, 0) as the initial approximation, then the first approximate solution is given by 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.

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).

Python example

[edit]
import numpy as np

ITERATION_LIMIT = 1000

# initialize the matrix
A = np.array([[10., -1., 2., 0.],
              [-1., 11., -1., 3.],
              [2., -1., 10., -1.],
              [0.0, 3., -1., 8.]])
# initialize the RHS vector
b = np.array([6., 25., -11., 15.])

# prints the system
print("System:")
for i in range(A.shape[0]):
    row = [f"{A[i, j]}*x{j + 1}" for j in range(A.shape[1])]
    print(f'{" + ".join(row)} = {b[i]}')
print()

x = np.zeros_like(b)
for it_count in range(ITERATION_LIMIT):
    if it_count != 0:
        print(f"Iteration {it_count}: {x}")
    x_new = np.zeros_like(x)

    for i in range(A.shape[0]):
        s1 = np.dot(A[i, :i], x[:i])
        s2 = np.dot(A[i, i + 1:], x[i + 1:])
        x_new[i] = (b[i] - s1 - s2) / A[i, i]
        if x_new[i] == x_new[i-1]:
          break

    if np.allclose(x, x_new, atol=1e-10, rtol=0.):
        break

    x = x_new

print("Solution: ")
print(x)
error = np.dot(A, x) - b
print("Error:")
print(error)

Weighted Jacobi method

[edit]

The weighted Jacobi iteration uses a parameter to compute the iteration as

with being the usual choice.[1] From the relation , this may also be expressed as

.

Convergence in the symmetric positive definite case

[edit]

In case that the system matrix is of symmetric positive-definite type one can show convergence.

Let be the iteration matrix. Then, convergence is guaranteed for

where is the maximal eigenvalue.

The spectral radius can be minimized for a particular choice of as follows where is the matrix condition number.

See also

[edit]

References

[edit]
  1. ^ Saad, Yousef (2003). Iterative Methods for Sparse Linear Systems (2nd ed.). SIAM. p. 414. ISBN 0898715342.
[edit]