← Back to Home

Iterative Methods: Part 2

Last time, we looked at 2D Poisson's equation and discussed how to arrive at a matrix equation using the finite difference method. The matrix, which represents the discrete Laplace operator, is sparse, so we can use an iterative method to solve the equation efficiently.

Today, we will look at Jacobi, Gauss-Seidel, Successive Over-Relaxation (SOR), and Symmetric SOR (SSOR), and a couple of related techniques—red-black ordering and Chebyshev acceleration. In addition, we will analyze the convergence of each method for the Poisson's equation, both analytically and numerically.

2. Classical iterative methods

Recall that we placed NN interior points along each axis. Since we did not know the values of ui,j:=u(xi,yj)u_{i,\,j} := u(x_{i},\,y_{j}) at N2N^{2} points, we had to solve N2N^{2} equations.

Discretize the square domain by placing N interior points along each axis. The points are marked in red, and their indices in green.

Each row in the equation Ku=fK\vec{u} = \vec{f} looked like,

ui1,jui+1,j+4ui,jui,j1ui,j+1=h2fij,\boxed{-u_{i-1,\,j} - u_{i+1,\,j} + 4u_{i,\,j} - u_{i,\,j-1} - u_{i,\,j+1} = h^{2}f_{ij}},

where hh is the mesh size:

h=1N+1.\displaystyle h = \frac{1}{N + 1}.

We also looked at the general equation Ax=bA\vec{x} = \vec{b}, and discussed splitting the matrix into a sum of two matrices:

A=PQ.A = P - Q.

Note that PP is nonsingular. This let us define an iterative method based on fixed-point method. Given an initial guess x0\vec{x}_{0}, we can solve the following equation to find the next iterate x1\vec{x}_{1}, then x2\vec{x}_{2}, and so on:

Pxk+1=Qxk+b.\boxed{P\vec{x}_{k + 1} = Q\vec{x}_{k} + \vec{b}}.

Lastly, we defined the matrix,

R:=P1Q,\boxed{R := P^{-1}Q},

and concluded that the iterative method converges from any initial guess, if and only if, the spectral radius ρ(R)\rho(R) is less than 1.

Before we move on, let us define the rate of convergence:

r:=lnρ(R).\boxed{r := -\text{ln}\,\rho(R)}.

The smaller the spectral radius of RR, the higher the rate of convergence. We will see that, for each method, the spectral radius takes the form of 1x1 - x for some x(0,1)x \in (0,\,1). Using Taylor expansion, we can write,

r=ln(1x)x.r = -\text{ln}(1 - x) \approx x.

When kk is sufficiently large, we can interpret 1/r1/r as the number of iterations further needed to reduce the error uku||\vec{u}_{k} - \vec{u}|| by a factor of ee.

c. Jacobi

For Jacobi method, we split ARn×nA \in \mathbb{R}^{n \times n} as follows:

{P:=DQ:=L+U,\left\{\begin{array}{l} P := D \\ Q := L + U, \end{array}\right.

where,

D=diagonals of A (assume nonzero diagonals)L=strictly lower triangular part of AU=strictly upper triangular part of A.\begin{array}{rcl} D & = & \text{diagonals of }A\text{ (assume nonzero diagonals)} \\ -L & = & \text{strictly lower triangular part of }A \\ -U & = & \text{strictly upper triangular part of }A. \end{array}

Let's look at the fixed-point equation entry-wise:

Dxk+1=(L+U)xk+baiixi(k+1)=jinaijxj(k)+bi.\begin{array}{c} D\vec{x}_{k + 1} = (L + U)\vec{x}_{k} + \vec{b} \\ \Updownarrow \\ \displaystyle a_{ii}x_{i}^{(k + 1)} = -\sum_{j\,\neq\,i}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}. \end{array}

Note that, for efficiency, we do not invert the matrix PP (i.e. use ** in Matlab) to solve the fixed-point equation. Instead, we consider the action of PP to deduce the action of P1P^{-1}. We can do so by writing the matrix equation entry-wise like we did above.

Since the diagonals are nonzero, we can divide both sides of the equation by aiia_{ii}. This is the action of P1P^{-1} for Jacobi. We can now compute xi(k+1)x_{i}^{(k + 1)}, the ii-th entry of xk+1\vec{x}_{k + 1}:

xi(k+1)=1aii[jinaijxj(k)+bi].\displaystyle\boxed{x_{i}^{(k + 1)} = \frac{1}{a_{ii}}\left[-\sum_{j\,\neq\,i}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}\right]}.

Notice how we use information from the kk-th iteration to determine the solution at the (k+1)(k + 1)-th iteration. The diagram below shows which entries we would use to find the 2nd entry of xk+1\vec{x}_{k + 1}.

In Jacobi method, we use the entries of the current iterate $$\vec{x}_{k}$$ to find those of the next $$\vec{x}_{k + 1}$$.

For Poisson's equation, the diagonals of KK are always 4, while the off-diagonals are either 0 or -1. Hence, Jacobi method simplifies to the following:

Jacobi, 2D Poisson's equation

ui,j(k+1)=14[ui1,j(k)+ui+1,j(k)+ui,j1(k)+ui,j+1(k)+h2fij]\displaystyle\boxed{u_{i,\,j}^{(k + 1)} = \frac{1}{4}\left[u_{i-1,\,j}^{(k)} + u_{i+1,\,j}^{(k)} + u_{i,\,j-1}^{(k)} + u_{i,\,j+1}^{(k)} + h^{2}f_{ij}\right]}

As we can see below, it's easy to implement Jacobi method.

% Set initial guess u
u = [...];
 
% Set temporary variable
uNew = zeros(N + 2);
 
for k = 1 : numIterations
  % Natural ordering
  for j = 2 : (N + 1)
    for i = 2 : (N + 1)
      uNew(i, j) = 1/4 * (u(i - 1, j) + u(i + 1, j) + u(i, j - 1) + u(i, j + 1) + h^2*f(i, j));
    end
  end
 
  % Update iterate
  u = uNew;
end

Finally, let's analyze the convergence of Jacobi method for Poisson's equation. We use the fact that the eigenvalues of KK are given by,

λi,j(K)=42cos(ihπ)2cos(jhπ),for i,j=1,,N.\lambda_{i,\,j}(K) = 4 - 2\cos(ih\pi) - 2\cos(jh\pi),\,\,\,\text{for }i,\,j = 1,\,\cdots,\,N.

Convergence of Jacobi, 2D Poisson's equation

ρ(R)=cos(hπ)112h2π2\displaystyle\boxed{\rho(R) = \cos(h\pi) \approx 1 - \frac{1}{2}h^{2}\pi^{2}}

Proof.

First, let's find the eigenvalues of R=P1QR = P^{-1}Q. Fortunately, we can write P=4IP = 4I and P1=14IP^{-1} = \frac{1}{4}I. Therefore,

R=14I(4IK)=I14K.\displaystyle R = \frac{1}{4}I \cdot (4I - K) = I - \frac{1}{4}K.

Hence, we can find the eigenvalues of RR from those of KK:

λi,j(R)=114λi,j(K)=12cos(ihπ)+12cos(jhπ).\displaystyle\begin{aligned} \lambda_{i,\,j}(R) &= 1 - \frac{1}{4}\lambda_{i,\,j}(K) \\ &= \frac{1}{2}\cos(ih\pi) + \frac{1}{2}\cos(jh\pi). \end{aligned}

The spectral radius ρ(R)\rho(R), which is the largest eigenvalue of RR in magnitude, occurs when ii and jj are both equal to 1 (or NN by symmetry). We use Taylor expansion to conclude that,

ρ(R)=cos(hπ)112h2π2.\displaystyle \rho(R) = \cos(h\pi) \approx 1 - \frac{1}{2}h^{2}\pi^{2}.

Since ρ(R)112h2π2<1\rho(R) \approx 1 - \frac{1}{2}h^{2}\pi^{2} < 1, Jacobi converges for Poisson's equation. Unfortunately, as we increase NN to refine the domain, the spectral radius approaches 1 quadratically. After some time, in order for Jacobi to reduce the error by ee, we need to perform

1r2π2(N+1)2\displaystyle\frac{1}{r} \,\approx\, \frac{2}{\pi^{2}}(N + 1)^{2}

additional iterations. Can we do better?

d. Gauss-Seidel

This time, we split AA as follows:

{P:=DLQ:=U,\left\{\begin{array}{l} P := D - L \\ Q := U, \end{array}\right.

where D,L,UD,\,L,\,U are defined in the exact manner as in Jacobi.

Let's analyze the action of P1P^{-1}:

(DL)xk+1=Uxk+bj=1iaijxj(k+1)=j=i+1naijxj(k)+bi.\begin{array}{c} (D - L)\vec{x}_{k + 1} = U\vec{x}_{k} + \vec{b} \\ \Updownarrow \\ \displaystyle \sum_{j\,=\,1}^{i}\,a_{ij}x_{j}^{(k + 1)} = -\sum_{j\,=\,i+1}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}. \end{array}

Now comes the clever idea. By the time we compute xi(k+1)x_{i}^{(k + 1)}, we will have computed the previous entries x0(k+1),,xi1(k+1)x_{0}^{(k + 1)},\,\cdots,\,x_{i - 1}^{(k + 1)}. These values are more accurate than those from the previous iteration x0(k),,xi1(k)x_{0}^{(k)},\,\cdots,\,x_{i - 1}^{(k)}, so we use them to compute xi(k+1)x_{i}^{(k + 1)}:

aiixi(k+1)=j=1i1aijxj(k+1)j=i+1naijxj(k)+bi.\displaystyle a_{ii}x_{i}^{(k + 1)} = -\sum_{j\,=\,1}^{i-1}\,a_{ij}x_{j}^{(k + 1)} - \sum_{j\,=\,i+1}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}.

Finally, we divide both sides of the equation by aii(0)a_{ii}\,(\neq 0):

xi(k+1)=1aii[j=1i1aijxj(k+1)j=i+1naijxj(k)+bi].\displaystyle \boxed{x_{i}^{(k + 1)} = \frac{1}{a_{ii}}\left[-\sum_{j\,=\,1}^{i-1}\,a_{ij}x_{j}^{(k + 1)} - \sum_{j\,=\,i+1}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}\right]}.

We can visualize the similarities and dissimilarities between Jacobi and Gauss-Seidel with the diagram below.

In Gauss-Seidel, we use the updated entries of $$\vec{x}_{k + 1}$$ to compute its remaining entries more accurately.

Gauss-Seidel, 2D Poisson's equation

ui,j(k+1)=14[ui1,j(k+1)+ui+1,j(k)+ui,j1(k+1)+ui,j+1(k)+h2fij]\displaystyle\boxed{u_{i,\,j}^{(k + 1)} = \frac{1}{4}\left[u_{i-1,\,j}^{(k + 1)} + u_{i+1,\,j}^{(k)} + u_{i,\,j-1}^{(k + 1)} + u_{i,\,j+1}^{(k)} + h^{2}f_{ij}\right]}

Convergence of Gauss-Seidel, 2D Poisson's equation

ρ(R)=cos2(hπ)1h2π2\boxed{\rho(R) = \cos^{2}(h\pi) \approx 1 - h^{2}\pi^{2}}

Proof.

We use the fact that, λ0\lambda \neq 0 is an eigenvalue of RJR_{J}, if and only if, λ2\lambda^{2} is an eigenvalue of RGSR_{GS}. This holds when the matrix AA is consistently ordered (I won't define this here).

For now, assume that KK is consistently ordered. Then,

ρ(RGS)=ρ(RJ)2(112h2π2)21h2π2.\begin{aligned} \rho(R_{GS}) &= \rho(R_{J})^{2} \\ &\approx \left(1 - \frac{1}{2}h^{2}\pi^{2}\right)^{2} \\ &\approx 1 - h^{2}\pi^{2}. \end{aligned}

The number of iterations further needed to reduce the error by ee is,

1r1π2(N+1)2,\displaystyle\frac{1}{r} \,\approx\, \frac{1}{\pi^{2}}(N + 1)^{2},

which is half of that of Jacobi.

Let's resolve the caveat. For our problem, KK can be consistently ordered if we traverse the points in a checkerboard fashion, also known as red-black ordering.

For optimal convergence, we use red-black ordering for Gauss-Seidel (and SOR).

The red points are updated first using the previous iterate, then the black points using the updated red points. Note that Gauss-Seidel does converge if we traverse the points in the natural order. However, we will not achieve optimal convergence. This will also be the case when we look at Successive Over-Relaxation.

We summarize Gauss-Seidel with a pseudocode below. Notice that we no longer need the temporary variable uNew.

% Set initial guess u
u = [...];
 
for k = 1 : numIterations
  % Red points
  for j = 2 : (N + 1)
    for i = 2 : (N + 1)
      if (mod(i + j, 2) == 0)
        u(i, j) = 1/4 * (u(i - 1, j) + u(i + 1, j) + u(i, j - 1) + u(i, j + 1) + h^2*f(i, j));
      end
    end
  end
 
  % Black points
  for j = 2 : (N + 1)
    for i = 2 : (N + 1)
      if (mod(i + j, 2) == 1)
        u(i, j) = 1/4 * (u(i - 1, j) + u(i + 1, j) + u(i, j - 1) + u(i, j + 1) + h^2*f(i, j));
      end
    end
  end
end

e. Successive Over-Relaxation

Gauss-Seidel takes the current iterate xk\vec{x}_{k} to the next xk+1\vec{x}_{k + 1} better than Jacobi. Why not take a step further in the direction that Gauss-Seidel points us to?

This is the motivation behind Successive Over-Relaxation (SOR). We take a weighted average of the current iterate xk\vec{x}_{k} and the next iterate xk+1,GS\vec{x}_{k + 1,\,GS} that Gauss-Seidel gives, to arrive at the final result xk+1\vec{x}_{k + 1}.

Entry-wise, this means the following:

xi(k+1)=(1ω)xi(k)+ωaii[j=1i1aijxj(k+1)j=i+1naijxj(k)+bi].\displaystyle \boxed{x_{i}^{(k + 1)} = (1 - \omega)\,x_{i}^{(k)} + \frac{\omega}{a_{ii}}\left[-\sum_{j\,=\,1}^{i-1}\,a_{ij}x_{j}^{(k + 1)} - \sum_{j\,=\,i+1}^{n}\,a_{ij}x_{j}^{(k)} + b_{i}\right]}.

Note that, if ω=1\omega = 1, we get Gauss-Seidel back.

In SOR, we take a step further in the direction (or its opposite) that Gauss-Seidel points us to.

The weight ω\omega cannot be arbitrary. We can show that, if SOR converges, then ω(0,2)\omega \in (0,\,2). In other words, we would never set ω0\omega \leq 0 or ω2\omega \geq 2. We also have a simple, sufficient condition for convergence:

Convergence of SOR

If the matrix AA is positive definite, that is,

xTAx>0,for allx0,\vec{x}^{T}A\vec{x} > 0,\,\,\,\text{for all}\,\,\vec{x} \neq \vec{0},

then SOR converges for any ω(0,2)\omega \in (0,\,2).

For our problem, we know that KK is positive definite because all of its eigenvalues are positive. While we can choose any ω(0,2)\omega \in (0,\,2) for convergence, the optimal value that leads to a fixed tolerance the quickest is given by,

ω=21+sin(hπ).\displaystyle\omega = \frac{2}{1 + \sin(h\pi)}.

Again, we want a consistently ordered KK for optimal convergence, so we will traverse the points using red-black ordering instead of natural ordering.

We summarize SOR for Poisson's equation below:

SOR, 2D Poisson's equation

{ω=21+sin(hπ)ui,j(k+1)=(1ω)ui,j(k)+ω4[ui1,j(k+1)+ui+1,j(k)+ui,j1(k+1)+ui,j+1(k)+h2fij]\boxed{\left\{\begin{array}{l} \displaystyle\omega = \frac{2}{1 + \sin(h\pi)} \\ \displaystyle u_{i,\,j}^{(k + 1)} = (1 - \omega)\,u_{i,\,j}^{(k)} + \frac{\omega}{4}\left[u_{i-1,\,j}^{(k + 1)} + u_{i+1,\,j}^{(k)} + u_{i,\,j-1}^{(k + 1)} + u_{i,\,j+1}^{(k)} + h^{2}f_{ij}\right] \end{array}\right.}

Convergence of SOR, 2D Poisson's equation

ρ(R)=cos2(hπ)(1+sin(hπ))212hπ\displaystyle\boxed{\rho(R) = \frac{\cos^{2}(h\pi)}{(1 + \sin(h\pi))^{2}} \approx 1 - 2h\pi}

This time, as we refine the domain, the spectral radius approaches 1 only linearly. The number of iterations further needed to reduce the error by ee is,

1r12π(N+1),\displaystyle\frac{1}{r} \,\approx\, \frac{1}{2\pi}(N + 1),

about NN times less than Jacobi and Gauss-Seidel!

f. Symmetric SOR

In Symmetric SOR (SSOR), we take a half-step of SOR in the natural order, then take another half-step of SOR in the reverse order. John Sheldon, who published SSOR in 1955, had gotten inspiration from "a computer which has magnetic tapes which read 'backward' as well as 'forward.'"

SSOR alone does not produce a solution much different from that from SOR. In fact, for every iteration of SSOR, we incur 2 iterations of SOR. However, when we consider an auxiliary problem known as Chebyshev acceleration, SSOR can reach convergence an order of magnitude faster. I won't explain why Chebyshev acceleration works, but will provide the pseudocode and implement it in our program.

% Set temporary variables
rho = 1 - pi/(2*N);
mu0 = 1;
mu1 = rho;
 
% Set initial guess u0
u0 = [...];
 
% Perform 1 iteration of SSOR
u1 = [...];
 
% Perform the remaining iterations
for k = 2 : numIterations
  mu = 1 / (2/(rho*mu1) - 1/mu0);
 
  % Perform 1 iteration of SSOR
  u = [...];
 
  % Apply Chebyshev acceleration
  u = 2*mu/(rho*mu1) * u - mu/mu0 * u0;
 
  % Update temporary variables
  mu0 = mu1;
  mu1 = mu;
  u0  = u1;
  u1  = u;
end

Since SSOR uses SOR, we need to select the weight ω\omega. For 2D Poisson's equation, David Young recommends

ω=21+22cos(hπ),\displaystyle\boxed{\omega = \frac{2}{1 + \sqrt{2 - 2\cos(h\pi)}}},

which results in the following spectral radius:

ρ(R)1π2N.\displaystyle\boxed{\rho(R) \approx 1 - \frac{\pi}{2N}}.

The rate of convergence does not seem to be different from that for SOR. However, with Chebyshev acceleration, we can multiply the error by μk\mu_{k} (denoted mu in the code) at the kk-th iteration. We have an upper bound for μk\mu_{k}:

μk21+kπ/N.\displaystyle\mu_{k} \leq \frac{2}{1 + k\sqrt{\pi/N}}.

3. Example

Recall that our model problem uses the RHS function,

f(x,y)=(a2+b2)π2sin(aπx)sin(bπy).f(x,\,y) = (a^{2} + b^{2})\pi^{2}\sin(a\pi x)\sin(b\pi y).

We choose a=1a = 1 and b=9b = 9 (the number of ridges in xx and yy-directions) and N=200N = 200. For each method, we graph the approximate solution after 100 iterations, and plot the error uku2||\vec{u}_{k} - \vec{u}||_{2} every 50 iterations. We will stop the method once kk reaches 500.

To compute the error uku2||\vec{u}_{k} - \vec{u}||_{2} at the kk-th iteration, we need to know the solution u\vec{u}. We can approximate it by considering the SSOR solution at 1000th iteration to be the exact solution, i.e. uu1000\vec{u} \,\approx\, \vec{u}_{1000}.

a. Results

After 100 iterations, we get the following solutions:

We can see that Gauss-Seidel is twice better than Jacobi. The amplitude of the Jacobi solution is about 0.40, while that of Gauss-Seidel is 0.64. The l2l^{2}-error, u100u2||\vec{u}_{100} - \vec{u}||_{2}, is about 61.0 for Jacobi, and 37.0 for Gauss-Seidel. SOR and SSOR produce even better result: an amplitude close to 1 and an l2l^{2}-error of about 3.3 and 0.0, respectively.

Next, let's check the behavior of the l2l^{2}-error uku2||\vec{u}_{k} - \vec{u}||_{2}. We take the base-10 logarithm of the error to focus on its magnitude.

From the slopes, we see that the error for Gauss-Seidel decreases twice as fast as that for Jacobi. However, even after 500 iterations, the errors remain large. In comparison, the error for SOR almost reaches 10610^{-6} (okay in practice) after 500 iterations, while that for SSOR reaches 101210^{-12} (true convergence) after 200 iterations! We may equate this to about 400 SOR iterations.

b. What if

I mentioned that, for optimal convergence, we want to follow red-black ordering for Gauss-Seidel and SOR, and to add Chebyshev acceleration to SSOR. Let's see what happens if we follow natural ordering for Gauss-Seidel and SOR, and perform SSOR without Chebyshev acceleration.

After 100 iterations, the SOR solution is perhaps graphically the most different. We see a funny oscillation on the right part of the domain, perhaps because we traversed the points from left to right. (I would have suspected the left side to be less accurate, and wonder if I reversed the graph by mistake.) The oscillation goes away as we increase the number of iterations, but is undesirable in the first place.

If we look at the convergence plot, we can understand even better why we should not use natural ordering for SOR or omit Chebyshev acceleration for SSOR.

This time, the error for SOR only reaches 10410^{-4} after 500 iterations. SSOR converges faster than SOR initially, but the error also reaches 10410^{-4} after 500 iterations. This is especially frustrating, since for each SSOR iteration, we performed 2 SOR iterations. Note that the choice of ω\omega is the same as before:

ω={21+sin(hπ),for SOR21+22cos(hπ),for SSOR\omega = \left\{\begin{array}{ll} \displaystyle\frac{2}{1 + \sin(h\pi)}, & \text{for SOR} \\ \displaystyle\frac{2}{1 + \sqrt{2 - 2\cos(h\pi)}}, & \text{for SSOR} \end{array}\right.

Notes

In short, use SSOR with Chebyshev acceleration. In addition to solving equations, SSOR makes a great preconditioner. Next time, we will look at Conjugate Gradient method, which is useful when the matrix is symmetric, positive definite (SPD).

You can find the code in its entirety here: Download from GitHub

References

  • James Demmel, Applied Numerical Linear Algebra (1997), 279-299.
  • Isaac Lee, lecture notes (2011).
  • John Sheldon, On the Numerical Solution of Elliptic Difference Equations, Mathematical Tables and Other Aids to Computation 9 (1955), 101-112.
  • Qiang Ye, lecture notes (2009).