x = cgs(A, b)
x = cgs(A, b, tol, maxit)
x = cgs(A, b, tol, maxit, M1, M2, x0)
[x, flag, relres, iter, resvec] = cgs(...)
| Parameter | Description |
|---|---|
| A | sparse square coefficient matrix. |
| b | right-hand side vector. |
| tol | relative residual tolerance. Default is 1e-6. |
| maxit | maximum number of iterations. |
| M1, M2 | optional preconditioners: sparse or full square matrices, diagonal vectors, or function handles that apply the preconditioner to one vector. |
| x0 | initial guess. |
| Parameter | Description |
|---|---|
| x | computed solution. |
| flag | 0 if convergence was reached, 1 if maxit was reached, 4 on numerical breakdown. |
| relres | relative residual norm. |
| iter | number of iterations performed. |
| resvec | residual norm history. |
cgs solves A*x = b using the conjugate gradients squared method.
The method is intended for sparse nonsymmetric systems. It supports sparse and full matrix preconditioners, diagonal vector preconditioners, and function handle preconditioners.
When M1 or M2 is a matrix, the solver applies it through an internal linear solve. A vector preconditioner is interpreted as the diagonal of a square preconditioner. A function handle preconditioner must accept one vector input and return a vector with the same length.
Sparse single and sparse single complex matrices are supported. If M1, M2, or x0 is complex, the computation uses the matching complex solver path.
A = sparse([4 1 0; 2 3 1; 0 1 2]);
b = [1; 2; 3];
[x, flag, relres, iter, resvec] = cgs(A, b, 1e-12, 20)
A = sparse([4 1 0; 2 3 1; 0 1 2]);
b = [1; 2; 3];
M = diag(diag(full(A)));
[x, flag] = cgs(A, b, 1e-12, 20, M)
A = sparse([4 1; 2 3]);
b = [5; 5];
M1 = [2 0; 0 1];
M2 = [2 0.5; 2 3];
[x, flag, relres, iter] = cgs(A, b, 1e-12, 10, M1, M2)
A = sparse(single([4 1i; 2 3]));
b = single([1; 2]);
[x, flag] = cgs(A, b, 1e-6, 20)
| Version | Description |
|---|---|
| 2.0.0 | initial version |
| 2.0.0 | sparse single and sparse single complex inputs, matrix preconditioners, and function handle preconditioners supported. |