Skip to content

gmres

sparse · scripts/sparse/gmres.m

: x = gmres (A, b, restart, tol, maxit, M1, M2, x0, …)
: x = gmres (A, b, restart, tol, maxit, M, [], x0, …)
: [x, flag, relres, iter, resvec] = gmres (A, b, …)

Solve the linear system A x = b by the generalised minimum residual method with restarts and optional left preconditioning.

Inputs:

  • A: the square system matrix, given either as a matrix or as a function (handle, inline or name) that returns A * x for a column x. Any parameters listed after x0 are passed on to that function.
  • b: the right-hand side, a column with as many rows as A.
  • restart: how many inner steps are taken before the Krylov space is discarded and the method restarts. Empty, or equal to the order of the system, means no restarts.
  • tol: the stopping tolerance on the preconditioned relative residual, norm (M \ (b - A x)) / norm (M \ b). Empty or absent means 1e-6.
  • maxit: the maximum number of outer iterations (restart cycles). Without restarts it is simply the maximum number of steps. When absent or empty it defaults to min (10, N / restart), so the total number of inner steps is bounded by restart * maxit, or by min (10, N) when both are empty.
  • M1, M2: the two factors of the preconditioner M = M1 * M2. Each may be a matrix or a function that returns M1 \ x (respectively M2 \ x). Leaving M1 empty or absent disables preconditioning. Preconditioning is applied from the left: the method works on M \ A x = M \ b.
  • x0: the starting vector. Empty or absent means a zero vector.

Outputs:

  • x: the approximate solution. If the method did not converge this is the iterate with the smallest preconditioned residual seen.
  • flag: why the method stopped.
    0

    the tolerance was met

    1

    the iteration limit was reached first

    2

    the preconditioner could not be applied (it is singular)

    3

    the iterates stopped changing (relative change below eps)

  • relres: the preconditioned relative residual of x.
  • iter: a two-element vector. iter(1) is the outer iteration (restart cycle) and iter(2) the inner step within that cycle at which x was produced, so x is iterate number (iter(1) - 1) * restart + iter(2). Without restarts iter(1) is 1. Because x is the best iterate rather than the last one, the total work done is numel (resvec) - 1 steps.
  • resvec: the preconditioned residual norm after every step, starting with the residual of x0.

Example: a stiff tridiagonal system, with and without preconditioning.

n = 20;
A = spdiags ([-ones(n,1), 4*ones(n,1), -ones(n,1)], -1:1, n, n);
b = A * ones (n, 1);
x = gmres (A, b);                      # default: at most 10 steps
x = gmres (A, b, 5, 1e-8, 20);         # restart every 5 steps
x = gmres (@(v) A * v, b, [], 1e-8, n); # A given as a function
[L, U] = ilu (A);
x = gmres (A, b, [], 1e-8, n, L, U);   # preconditioner as two factors
x = gmres (A, b, [], 1e-8, n, @(v) U \ (L \ v)); # or as one function

Example: extra parameters reach the operator function. Here the system solved is A^p x = b with p = 2.

function y = apply_power (A, v, p)
  y = v;
  for k = 1:p
    y = A * y;
  endfor
endfunction
x = gmres (@(v, p) apply_power (A, v, p), b, [], [], [], [], [], [], 2);

Since the preconditioner is applied from the left, calling gmres (A, b, [], [], k, M) produces the same iterates as