Lecture 23
Solve Ax = d: Generalized Minimum Residual Method
(Lecture notes taken by Zhibin Deng and Xiaoyin Ji)
ο·
Comparison of Block Tri-diagonal (BT), Conjugate Gradient (CG) and
Generalized Minimum Residual (GMRES)
Table 1: Algorithm Comparison
nx=ny=nz
L_2 Error
6
11
21
41
4.9582
1.2461
0.3120
0.0782
Compute Time (seconds)
BT
CG
GMRES
1.6104
93.2287
2.8129
207.0849
2.9496
??
The result is obtained in MATLAB 2010, Core i-7-920.
Remark:
1. Block Tri-diagonal Algorithm requires the matrix form is block tri-diagonal.
However, there are less linear solvers in it, hence the compute time is less. This
method is efficient if the matrix is sparse.
2. Conjugate Gradient method requires the matrix form is symmetric positive definite.
It only needs to store the last direction.
3. GMRES has no special requirement in matrix form, however, it has to store all
directions in the process.
ο·
Krylov Space.
Definition: Subspace π¦m+1 spanned by vectors {π 0 , π΄π 0 , π΄2 π 0 , π΄3 π 0 , … , π΄π π 0 } is called
Krylov space, where π 0 = π − π΄π₯ 0 is an initial vector.
Note that π΄π₯ = π ⇔ π(π₯) = π − π΄π₯ = 0 ⇔ ππππ¦ π π (π¦)π(π¦) = 0
The main idea of GMRES algorithm is to approximate the solution of system π΄π₯ = π by a
linear combination of Krylov vectors π΄π π 0. Define π₯ π+1 = π₯ 0 + πΌ0 π 0 + πΌ1 π΄π 0 + β― +
πΌπ π΄π π 0. We need to find πΌ = (πΌ0 , … , πΌπ ) such that π π (π₯ π+1 )π(π₯ π+1 ) is minimized.
π 0
Let π¦ = π₯ 0 + π¦π+1 , and π¦m+1 = {π§|π§ = ∑π
π=0 πΌπ π΄ π }, then
π π (π₯ π+1 )π(π₯ π+1 ) = ππππ¦∈π₯ 0 +π¦π+1 π π (π¦)π(π¦)
Note:
π(π₯ π+1 ) = π − π΄π₯ π+1
= π − π΄(π₯ 0 + πΌ0 π 0 + πΌ1 π΄π 0 + β― + πΌπ π΄π π 0 )
= [πΌ − (πΌ0 π΄ + πΌ1 π΄2 + β― + πΌπ π΄π+1 )]π 0.
Let ππ+1 (π΄) = πΌ − (πΌ0 π΄ + πΌ1 π΄2 + β― + πΌπ π΄π+1 ), which is polynomial of matrix π΄.
When matrix π΄ is invertible and π = π − 1, consider matrix π΄’s characteristic polynomial.
Then, π(π΄) = 0, and we have a solution r(x) = 0. However, generally we have π βͺ π when
algorithm terminates.
ο·
Hessenburg matrix.
--efficient method for finding πΌ
π×π
β = [π΄π 0 , … , π΄π−1 π 0 ]
π
π×π π×π π×1
π×1
β π
β
π΄
πΌ = πβ0 , πΌ ∈ βπ , π β« π
π β
π₯ π = π₯ 0 + ππ
This is a LS (least square) problem. We use QR to solve the normal equation. In order to deal
with that, we replace ππ by ππ , whose columns π£π s are orthogonal. For example,
Col 1: π 0 = ππ£1
π£1 π π£1 = 1
π
π = √π 0 π 0
π£1 = π 0 /π
Col. 2: π΄π¦1 ⊂ π¦2
π΄π£1 = π£1 β11 + π£2 β21
π£1 π π΄π£1 = π£1 π π£1 β11 + π£1 π π£2 β21 = β11
π§ = π΄π£1 − π£1 β11 = π£2 β21 , and note π£2 π π£2 = 1
2
π§ π π§ = β21
β21 = √π§ π π§
π£2 = π§/β21
π΄[π£1 ] = [π£1
π£2 ] [β11 ]
β21
Col. 3: π΄π¦ ⊂ π¦3
π΄π£2 = π£1 β12 + π£2 β22 + π£3 β32
π΄[π£1
π£2 ] = β
[π£1
π£2
β11
π£3 ] [β21
π×3
β
β12
β22 ]
β32
3×2
...
π΄[π£1
β― π£π ] = [π£1
β―
π£π
β±
π£π+1 ] [ β±
β
β±
β±
]
β±
π+1×π π»ππ π ππππππ
Reduction Theorem: The following are equivalent
1. LS problem π΄ππ πΌ = π 0 has solution π₯ π+1 = ππ πΌ + π₯ 0
2. LS problem π΄ππ π½ = ππ+1 π1 π has solution π₯ π+1 = ππ π½ + π₯ 0
3. LS problem π»π½ = π1 π has solution π₯ π+1 = ππ π½ + π₯ 0
Note that we need matrix ππ to compute new iteration solution π₯ π+1 , that’s why we have to
store all the directions π£j .
ο·
Givens transforms and QR factors.
According to Reduction Theorem, we convert the LS problem π΄ππ πΌ = π 0 into a new LS
problem π»π½ = π1 π. Note that π» is a Hessenberg matrix, we can use Givens rotations to solve the
new LS problem easily if π βͺ π.
ο·
GMRES algorithm.
π
Choose initial guess π₯ 0 , let π 0 = π − π΄π₯ 0 , π = √π 0 π 0 , π(: ,1) = π 0 /π,
for k = 1 to maxm
π(: , π + 1) = π΄π(: , π)
Compute the columns of π(: , π + 1) πππ π»(: , π + 1)
Compute the QR factors of H
Test for convergence
end
π₯ π+1 = π₯ 0 + ππ½
gmresfull.m Code:
clear;
% Solves a block tridiagonal non SPD from the finite difference method of
%
- u_xx - u_yy + a1 u_x + a2 u_y + c u = f(x,y)
% with zero boundary conditions.
% Uses the generalized minimum residual method.
% Define the NxN coefficient matrix AA where N = n^2.
n = 10; N = n*n;
errtol = 0.001;
kmax = 200;
A = zeros(n); a1 = 10; a2 = 1; c = 1; h = 1./(n+1);
for i = 1:n
A(i,i) = 4 + c*h*h + a1*h + a2*h;
if (i>1)
A(i,i-1) = -1 - a1*h ;
end
if (i<n)
A(i,i+1) = -1;
end
end
I = eye(n);
II = I + I*a2*h;
AA = zeros(N);
for i = 1:n
newi = (i-1)*n + 1;
lasti = i*n;
AA(newi:lasti,newi:lasti) = A;
if (i>1)
AA(newi:lasti,newi-n:lasti-n) = -II;
end
if (i<n)
AA(newi:lasti,newi+n:lasti+n) = -I;
end
end
u = zeros(N,1);
r = zeros(N,1);
for j = 1:n
for i = 1:n
I = (j-1)*n + i;
r(I)= h*h*2000*(1+sin(pi*(i-1)*h)*sin(pi*(j-1)*h));
end
end
rho = (r'*r)^.5;
errtol = errtol*rho;
g = rho*eye(kmax+1,1);
v(:,1) = r/rho;
k = 0;
% Begin gmres loop.
while((rho > errtol) & (k < kmax))
k = k+1;
% Matrix vector product.
v(:,k+1) = AA*v(:,k);
% Begin modified GS. May need to reorthogonalize.
for j = 1:k
h(j,k) = v(:,j)'*v(:,k+1);
v(:,k+1) = v(:,k+1) - h(j,k)*v(:,j);
end
h(k+1,k) = (v(:,k+1)'*v(:,k+1))^.5;
if (h(k+1,k) ~= 0)
v(:,k+1) = v(:,k+1)/h(k+1,k);
end
% Apply old Givens rotations to h(1:k,k).
if k>1
for i=1:k-1
hik
= c(i)*h(i,k)-s(i)*h(i+1,k);
hipk
= s(i)*h(i,k)+c(i)*h(i+1,k);
h(i,k)
= hik;
h(i+1,k)
= hipk;
end
end
normh = norm(h(k:k+1,k));
% May need better Givens implementation.
% Define and apply new Givens rotations to h(k:k+1,k).
if normh ~= 0
c(k)
= h(k,k)/normh;
s(k)
= -h(k+1,k)/normh;
h(k,k)
= c(k)*h(k,k) - s(k)*h(k+1,k);
h(k+1,k)
= 0;
gk
= c(k)*g(k) - s(k)*g(k+1);
gkp
= s(k)*g(k) + c(k)*g(k+1);
g(k)
= gk;
g(k+1)
= gkp;
end
rho = abs(g(k+1));
mag(k) = rho;
end
% End of gmres loop.
% h(1:k,1:k) is upper triangular matrix in QR.
y = h(1:k,1:k)\g(1:k);
% Form linear combination.
for i = 1:k
u(:) = u(:) + v(:,i)*y(i);
end
k
figure(1)
semilogy(mag)
for j = 1:n
for i = 1:n
I = (j-1)*n + i;
u2d(i,j) = u(I);
end
end
figure(2)
uu2d = zeros(n+2);
uu2d(2:n+1,2:n+1) = u2d;
mesh(uu2d)