# gmres

Solve a system of linear equations with the generalized minimum residual method.

## Syntax

x = gmres(A,b)

x = gmres(A,b,restart,rtol)

x = gmres(A,b,restart,rtol,maxit)

x = gmres(A,b,restart,rtol,maxit,M1)

x = gmres(A,b,restart,rtol,maxit,M1,M2)

x = gmres(A,b,restart,rtol,maxit,M1,M2,x0)

[x, iflag, relres, iter, resvec] = gmres(...)

## Inputs

`A`- The left-hand side matrix, or a function name or handle to generate A*x.
`b`- The right-hand side vector.
`restart`- The number of steps or inner iterations between restarts of the algorithm (default: size(b,1)).
`rtol`- The relative convergerce tolerance (default: 1.0e-6).
`maxit`- The maximum number of restarts or outer iterations (default: 10). See Comments.
`M1`- The left preconditioner matrix, or a function name or handle to generate M1\x. (default: none or [])
`M2`- The right preconditioner matrix, or a function name or handle to generate M2\x. (default: none or [])
`x0`- The initial estimate of the solution (default: zeroes(length(b),1)).

## Outputs

- x
- The solution vector.
- iflag
- The termination status.
- iflag = 0:
- The algorithm converged within the allowed tolerance and number of iterations.
- iflag = 1:
- The algorithm completed the allowed iterations without converging.
- iflag = 2:
- A preconditioner matrix was singular.
- iflag = 3:
- The algorithm converged, but not at a solution due to stagnation.

- relres
- The magnitude of the residual relative to norm
`(b)`. - iter
- The outer and inner iteration numbers at which the solution was found.
- resvec
- The residual vector.

## Example

Case using a function handle for A and a left preconditioner.

```
A = [3, 1, 0; 0, 5, 6; -2, 0, 4];
b = [13; 14; -10];
tol = 0.0001;
max_it = 5;
M1 = diag(diag(A));
M2 = [];
x0 = [];
Ahandle = @(x) A * x;
[x, iflag, relres, iter, resvec] = gmres(Ahandle, b, [], tol, max_it, M1, M2, x0)
```

```
x = [Matrix] 3 x 1
3.00000
4.00000
-1.00000
iflag = 0
relres = 3.85757e-16
iter = [Matrix] 1 x 2
1 3
resvec = [Matrix] 4 x 1
5.73304e+00
3.10450e+00
6.03540e-02
2.21156e-15
```

## Comments

The purpose of the restart strategy is to reduce the storage and computational requirements of the algorithm. When restart is [] or size(b,1), no restart occurs and the total number of steps taken is no more than min(maxit, size(b,1)). The more common situation is to specify restart, size(b,1), in which case the limit on the number is steps is min(maxit * restart, size(b,1)).