Solves the linear system of equations a
*
x=
b by means of the Preconditioned Conjugate Gradient iterative method. The input arguments are
- a can be either a square (preferably sparse) matrix or a function handle, inline function or string containing the name of a function which computes a
*
x. In principle a should be symmetric and positive definite; ifpcg
finds a to not be positive definite, you will get a warning message and the flag output parameter will be set.- b is the right hand side vector.
- tol is the required relative tolerance for the residual error, b
-
a*
x. The iteration stops ifnorm (
b-
a*
x) <=
tol* norm (
b-
a*
x0)
. If tol is empty or is omitted, the function sets tol= 1e-6
by default.- maxit is the maximum allowable number of iterations; if
[]
is supplied formaxit
, orpcg
has less arguments, a default value equal to 20 is used.- m = m1 * m2 is the (left) preconditioning matrix, so that the iteration is (theoretically) equivalent to solving by
pcg
P*
x=
m\
b, with P=
m\
a. Note that a proper choice of the preconditioner may dramatically improve the overall performance of the method. Instead of matrices m1 and m2, the user may pass two functions which return the results of applying the inverse of m1 and m2 to a vector (usually this is the preferred way of using the preconditioner). If[]
is supplied for m1, or m1 is omitted, no preconditioning is applied. If m2 is omitted, m = m1 will be used as preconditioner.- x0 is the initial guess. If x0 is empty or omitted, the function sets x0 to a zero vector by default.
The arguments which follow x0 are treated as parameters, and passed in a proper way to any of the functions (a or m) which are passed to
pcg
. See the examples below for further details. The output arguments are
- x is the computed approximation to the solution of a
*
x=
b.- flag reports on the convergence. flag
= 0
means the solution converged and the tolerance criterion given by tol is satisfied. flag= 1
means that the maxit limit for the iteration count was reached. flag= 3
reports that the (preconditioned) matrix was found not positive definite.- relres is the ratio of the final residual to its initial value, measured in the Euclidean norm.
- iter is the actual number of iterations performed.
- resvec describes the convergence history of the method. resvec
(i,1)
is the Euclidean norm of the residual, and resvec(i,2)
is the preconditioned residual norm, after the (i-1)-th iteration, i= 1, 2, ...,
iter+1
. The preconditioned residual norm is defined asnorm (
r) ^ 2 =
r' * (
m\
r)
where r=
b-
a*
x, see also the description of m. If eigest is not required, only resvec(:,1)
is returned.- eigest returns the estimate for the smallest eigest
(1)
and largest eigest(2)
eigenvalues of the preconditioned matrix P=
m\
a. In particular, if no preconditioning is used, the estimates for the extreme eigenvalues of a are returned. eigest(1)
is an overestimate and eigest(2)
is an underestimate, so that eigest(2) /
eigest(1)
is a lower bound forcond (
P, 2)
, which nevertheless in the limit should theoretically be equal to the actual value of the condition number. The method which computes eigest works only for symmetric positive definite a and m, and the user is responsible for verifying this assumption.Let us consider a trivial problem with a diagonal matrix (we exploit the sparsity of A)
N = 10; A = diag (sparse([1:N])); b = rand (N, 1); [L, U, P, Q] = luinc (A,1.e-3);Example 1: Simplest use of
pcg
x = pcg(A,b)Example 2:
pcg
with a function which computes a*
xfunction y = applyA (x) y = [1:N]'.*x; endfunction x = pcg ("applyA", b)Example 3:
pcg
with a preconditioner: l * ux=pcg(A,b,1.e-6,500,L*U);Example 4:
pcg
with a preconditioner: l * u. Faster than Example 3 since lower and upper triangular matrices are easier to invertx=pcg(A,b,1.e-6,500,L,U);Example 5: Preconditioned iteration, with full diagnostics. The preconditioner (quite strange, because even the original matrix a is trivial) is defined as a function
function y = applyM(x) K = floor (length (x) - 2); y = x; y(1:K) = x(1:K)./[1:K]'; endfunction [x, flag, relres, iter, resvec, eigest] = ... pcg (A, b, [], [], "applyM"); semilogy (1:iter+1, resvec);Example 6: Finally, a preconditioner which depends on a parameter k.
function y = applyM (x, varargin) K = varargin{1}; y = x; y(1:K) = x(1:K)./[1:K]'; endfunction [x, flag, relres, iter, resvec, eigest] = ... pcg (A, b, [], [], "applyM", [], [], 3)References
[1] C.T.Kelley, 'Iterative methods for linear and nonlinear equations', SIAM, 1995 (the base PCG algorithm)
[2] Y.Saad, 'Iterative methods for sparse linear systems', PWS 1996 (condition number estimate from PCG) Revised version of this book is available online at http://www-users.cs.umn.edu/~saad/books.html
The following code
# Simplest usage of pcg (see also 'help pcg') N = 10; A = diag ([1:N]); b = rand (N, 1); y = A \ b; #y is the true solution x = pcg (A, b); printf('The solution relative error is %g\n', norm (x - y) / norm (y)); # You shouldn't be afraid if pcg issues some warning messages in this # example: watch out in the second example, why it takes N iterations # of pcg to converge to (a very accurate, by the way) solution
Produces the following output
warning: pcg: maximum number of iterations (10) reached warning: the initial residual norm was reduced 9.67486e+16 times. The solution relative error is 2.01046e-16
The following code
# Full output from pcg, except for the eigenvalue estimates # We use this output to plot the convergence history N = 10; A = diag ([1:N]); b = rand (N, 1); X = A \ b; #X is the true solution [x, flag, relres, iter, resvec] = pcg (A, b); printf('The solution relative error is %g\n', norm (x - X) / norm (X)); title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||/||b||)'); semilogy([0:iter], resvec / resvec(1),'o-g'); legend('relative residual');
Produces the following output
The solution relative error is 1.45414e-16
and the following figure
![]() |
The following code
# Full output from pcg, including the eigenvalue estimates # Hilbert matrix is extremely ill conditioned, so pcg WILL have problems N = 10; A = hilb (N); b = rand (N, 1); X = A \ b; #X is the true solution [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], 200); printf('The solution relative error is %g\n', norm (x - X) / norm (X)); printf('Condition number estimate is %g\n', eigest(2) / eigest (1)); printf('Actual condition number is %g\n', cond (A)); title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||)'); semilogy([0:iter], resvec,['o-g';'+-r']); legend('absolute residual','absolute preconditioned residual');
Produces the following output
The solution relative error is 4.64537e-05 Condition number estimate is 1.60196e+13 Actual condition number is 1.60247e+13
and the following figure
![]() |
The following code
# Full output from pcg, including the eigenvalue estimates # We use the 1-D Laplacian matrix for A, and cond(A) = O(N^2) # and that's the reasone we need some preconditioner; here we take # a very simple and not powerful Jacobi preconditioner, # which is the diagonal of A N = 100; A = zeros (N, N); for i=1 : N - 1 # form 1-D Laplacian matrix A (i:i+1, i:i+1) = [2 -1; -1 2]; endfor b = rand (N, 1); X = A \ b; #X is the true solution maxit = 80; printf('System condition number is %g\n', cond (A)); # No preconditioner: the convergence is very slow! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit); printf('System condition number estimate is %g\n', eigest(2) / eigest(1)); title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||)'); semilogy([0:iter], resvec(:,1), 'o-g'); legend('NO preconditioning: absolute residual'); pause(1); # Test Jacobi preconditioner: it will not help much!!! M = diag (diag (A)); # Jacobi preconditioner [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M); printf('JACOBI preconditioned system condition number estimate is %g\n', eigest(2) / eigest(1)); hold on; semilogy([0:iter], resvec(:,1), 'o-r'); legend('NO preconditioning: absolute residual', ... 'JACOBI preconditioner: absolute residual'); pause(1); # Test nonoverlapping block Jacobi preconditioner: it will help much! M = zeros (N, N); k = 4; for i = 1 : k : N # form 1-D Laplacian matrix M (i:i+k-1, i:i+k-1) = A (i:i+k-1, i:i+k-1); endfor [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M); printf('BLOCK JACOBI preconditioned system condition number estimate is %g\n', eigest(2) / eigest(1)); semilogy ([0:iter], resvec(:,1),'o-b'); legend('NO preconditioning: absolute residual', ... 'JACOBI preconditioner: absolute residual', ... 'BLOCK JACOBI preconditioner: absolute residual'); hold off;
Produces the following output
System condition number is 4133.64 System condition number estimate is 4132.97 JACOBI preconditioned system condition number estimate is 4132.97 BLOCK JACOBI preconditioned system condition number estimate is 1034.61
and the following figure
![]() |