The Jacobi method is the simplest of the iterative methods, and relies on the fact that the matrix is diagonally dominant. Starting from the problem definition:
we decompose in to , where is lower triangular, is diagonal, is upper triangular.
We then assume that we have an initial guess at the solution , and try to find a new estimate . Assuming that the diagonal dominates over and , a sensible choice would be to insert and the unknown into the equation like so:
we can rearrange to get an equation for . This is easily solved as we can take the inverse of the diagonal matrix by simply inverting each diagonal element individually:
Thus we end up with the general Jacobi iteration:
The Jacobi method is an example of a relaxation method, where the matrix is split into a dominant part (which is easy to solve), and the remainder . That is,
This can be rearranged in terms of the residual to the update equation
For the Jacobi method and . Other relaxation methods include Gauss-Seidel, where and , and successive over-relaxation (SOR), where and , where is the relaxation parameter that is within the range .
For any relaxation method to converge we need , where is the spectral radius of , which is defined as the largest eigenvalue of a a given matrix :
For the SOR method, the relaxation parameter is generally chosen to minimise , so that the speed of convergence is maximised. In some cases this optimal is known, for example for finite difference discretisation of the Poisson equation. However, in many cases sophisticated eigenvalue analysis is required to determine the optimal .
This exercise involves the manipulation and solution of the linear system resulting from
the finite difference solution to Poisson's equation in two dimensions. Let be a
sparse symmetric positive definite matrix of dimension created
using scipy.sparse
(for a given ) by the function
buildA
as follows:
import numpy as np
import scipy.sparse as sp
def buildA(N):
dx = 1 / N
nvar = (N - 1)**2;
e1 = np.ones((nvar), dtype=float);
e2 = np.copy(e1)
e2[::N-1] = 0
e3 = np.copy(e1)
e3[N-2::N-1] = 0
A = sp.spdiags(
(-e1, -e3, 4*e1, -e2, -e1),
(-(N-1), -1, 0, 1, N-1), nvar, nvar
)
A = A / dx**2;
return A
and let and be the vectors defined in
buildf1
and buildf2
def buildf1(N):
x = np.arange(0, 1, 1/N).reshape(N, 1)
y = x.T
f = np.dot(np.sin(np.pi*x), np.sin(np.pi*y))
return f[1:,1:].reshape(-1,1)
def buildf2(N):
x = np.arange(0, 1, 1/N).reshape(N, 1)
y = x.T
f = np.dot(np.maximum(x,1-x), np.maximum(y,1-y))
return f[1:,1:].reshape(-1, 1)
We will consider manipulation of the matrix and solution of the linear systems . The solution to this linear system corresponds to a finite difference solution to Poisson's equation on the unit square with zero Dirichlet boundary conditions where is either or . PDEs of this type occur (usually with some additional reaction and or convection terms) very frequently in mathematical modelling of physiological processes, and even in image analysis.
import numpy as np
import scipy.sparse as sp
import scipy.sparse.linalg
import scipy.optimize
import matplotlib.pylab as plt
def jacobi(A, b, x0=None, tol=1e-5, max_iter=1000):
if x0 is None:
x0 = np.zeros_like(b)
x = np.copy(x0)
b_norm = np.linalg.norm(b)
# jacobi method: M = D
M = A.diagonal().reshape(-1, 1)
invM = 1/M
# main relaxation iteration
for i in range(max_iter):
r = b - A @ x
error = np.linalg.norm(r) / b_norm
if error < tol:
break
x += invM * r
return x, i
num = 20
iterations = np.empty((num, 2), dtype=int)
iterations[:] = np.nan
Ns = np.logspace(0.5, 1.5, num=num, dtype=int)
for j, buildf in enumerate((buildf1, buildf2)):
for i, N in enumerate(Ns):
A = buildA(N)
f = buildf(N)
max_iter = 10*N
x, iters = jacobi(A, f, max_iter=max_iter)
if iters < max_iter:
iterations[i, j] = iters
plt.plot(Ns, iterations)
plt.xlabel('N')
plt.ylabel('iterations')
plt.show()
scipy.optimize.minimize_scalar
.def SOR(A, b, omega, x0=None, tol=1e-5, max_iter=300):
if x0 is None:
x0 = np.zeros_like(b)
x = np.copy(x0)
b_norm = np.linalg.norm(b)
# SOR method
D = sp.spdiags((A.diagonal()), (0), *A.shape)
L = sp.tril(A, k=-1)
M = (1/omega) * D + L
# main relaxation iteration
for i in range(max_iter):
r = b - A @ x
error = np.linalg.norm(r) / b_norm
if error < tol:
break
x += sp.linalg.spsolve_triangular(M, r)
return x, i
N = 64
A = buildA(N)
f = buildf2(N)
def SOR_iterations(omega):
x, i = SOR(A, f, omega, max_iter=10, tol=1e-32)
return np.linalg.norm(A @ x - f)
res = scipy.optimize.minimize_scalar(SOR_iterations, bracket=[0.1, 1.0, 1.99], tol=1e-2)
print('ideal omega is', res.x, 'versus analytic value of', 2 / (1 + np.sin(np.pi/N)))