7.1. Jacobi method#
The Jacobi method named after German mathematician Carl Jacobi is the simplest indirect method. Given a system of linear equations of the form \(A \vec{x} = \vec{b}\), splitting the coefficient matrix \(A\) into the of elements from the lower triangular, diagonal and upper triangular parts of \(A\) to form matrices \(L\), \(D\) and \(U\) such that \(A = L + D + U\), e.g.,
Rewriting the linear system \(A\vec{x}=\vec{b}\) using \(L\), \(D\) and \(U\) gives
Let the \(\vec{x}\) on the left-hand side be \(\vec{x}^{(k+1)}\) and the \(\vec{x}\) on the right-hand side be \(\vec{x}^{(k)}\) then
and writing this out for each element gives the Jacobi method gives the following definition of the Jacobi method.
(The Jacobi method)
The Jacobi method for solving a system of linear equations of the form \(A \vec{x} = \vec{b}\) is
7.1.1. The iteration matrix for the Jacobi method#
The iteration matrix for the Jacobi method can be determined by rearranging equation (7.2)
and comparing to equation (7.1) we get the iteration matrix for the Jacobi method
7.1.2. The residual#
The Jacobi method is applied by iterating equation (7.3) until the estimate \(\vec{x}^{(k+1)}\) is accurate enough for our needs. Since we do not know what the exact solution is, we need a way to estimate the error in our approximations. Since \(\vec{x}^{(k)}\) is an approximation of the exact solution \(\vec{x}\) then if \(\vec{e}^{(k)}\) is the error of the \(k\)th iteration we have \(\vec{x} = \vec{x}^{(k)} + \vec{e}^{(k)}\). Substituting this into the linear system \(A\vec{x} = \vec{b}\) and rearranging gives
Let \(\vec{r}^{(k)} = A\vec{e}^{(k)}\) be an \(n \times 1\) column vector known as the residual.
(The residual)
The residual for the system of linear equations \(A \vec{x}^{(k)} = \vec{b}\) is
As \(\vec{x}^{(k)} \to \vec{x}\), \(\vec{r} \to 0\) so we can use the following convergence criteria
where \(tol\) is some small number. The smaller the value of \(tol\) the closer \(\vec{x}^{(k)}\) is to the exact solution but of course this will require more iterations. In practice a compromise is made between the accuracy required and the computational resources available. Typical values of \(tol\) are around \(10^{-4}\) or \(10^{-6}\).
Calculate the solution to the following system of linear equations using the Jacobi method with an accuracy tolerance of \(tol = 10^{-4}\)
Solution (click to show)
The Jacobi method for this system is
Using starting values of \(\vec{x} = \vec{0}\). Calculating the first iteration
Calculate the residual
Since \(\max(\| \vec{r}^{(1)} \|) = 6.0 > 10^{-4}\) we continue iterating. Calculating the second iteration
Calculate the residual
Since \(\max(\| \vec{r}^{(2)} \|) = 5.0 > 10^{-4}\) we continue iterating. The Jacobi method was iterated until \(\max(\|\vec{r}\|) < 10^{-4}\) and a selection of the iteration values are given in the table below.
\(k\) |
\(x_{1}\) |
\(x_{2}\) |
\(x_{3}\) |
\(\max(\|\vec{r}\|)\) |
---|---|---|---|---|
0 |
0.000000 |
0.000000 |
0.000000 |
14.000000 |
1 |
-0.500000 |
-2.000000 |
3.500000 |
6.00e+00 |
2 |
1.000000 |
-0.750000 |
3.000000 |
5.00e+00 |
3 |
0.062500 |
-2.000000 |
3.312500 |
3.75e+00 |
4 |
1.000000 |
-1.218750 |
3.000000 |
3.12e+00 |
5 |
0.414062 |
-2.000000 |
3.195312 |
2.34e+00 |
\(\vdots\) |
\(\vdots\) |
\(\vdots\) |
\(\vdots\) |
\(\vdots\) |
48 |
1.000000 |
-1.999975 |
3.000000 |
1.01e-04 |
49 |
0.999981 |
-2.000000 |
3.000006 |
7.57e-05 |
So the Jacobi method took 49 iterations to converge to the solution \(x_1 =1\), \(x_2 =-2\) and \(x_3 = 3\).
7.1.3. Code#
The code below defines a function called jacobi()
which solves a linear system of equations of the form \(A \vec{x} = \vec{b}\) using the Jacobi method.
import numpy as np
def jacobi(A, b, tol=1e-6):
n = len(b)
x = np.zeros(n)
maxiter = 100
for k in range(maxiter):
xold = np.copy(x)
for i in range(n):
sum_ = 0
for j in range(n):
if i != j:
sum_ += A[i,j] * xold[j]
x[i] = (b[i] - sum_) / A[i,i]
r = b - np.dot(A, x)
if max(abs(r)) < tol:
break
return x
# Define system
A = np.array([[4, 3, 0], [3, 4, -1], [0, -1, 4]])
b = np.array([-2, -8, 14])
# Solve system
x = jacobi(A, b, tol=1e-4)
print(f"x = {x}")
% Define system
A = [4, 3, 0 ;
3, 4, -1 ;
0, -1, 4 ];
b = [-2 ; -8 ; 14];
% Solve system
tol = 1e-4;
x = jacobi(A, b, tol)
% --------------------------------------------------------------
function x = jacobi(A, b, tol)
n = length(b);
x = zeros(n, 1);
maxiter = 100;
for k = 1 : 100
xold = x;
for i = 1 : n
sum_ = 0;
for j = 1 : n
if j ~= i
sum_ = sum_ + A(i,j) * xold(j);
end
end
x(i) = (b(i) - sum_) / A(i,i);
end
r = b - A * x;
if max(abs(r)) < tol
break
end
end
end