MATLAB Function Reference | Search  Help Desk |
cgs | Examples See Also |
Conjugate Gradients Squared method
x = cgs(A,b) cgs(A,b,tol) cgs(A,b,tol,maxit) cgs(A,b,tol,maxit,M) cgs(A,b,tol,maxit,M1,M2) cgs(A,b,tol,maxit,M1,M2,x0) x = cgs(A,b,tol,maxit,M1,M2,x0) [x,flag] = cgs(A,b,tol,maxit,M1,M2,x0) [x,flag,relres] = cgs(A,b,tol,maxit,M1,M2,x0) [x,flag,relres,iter] = cgs(A,b,tol,maxit,M1,M2,x0) [x,flag,relres,iter,resvec] = cgs(A,b,tol,maxit,M1,M2,x0)
x = cgs(A,b)
attempts to solve the system of linear equations A*x = b
for x
. The coefficient matrix A
must be square and the right hand side (column) vector b
must have length n
, where A
is n
-by-n
. cgs
will start iterating from an initial estimate that by default is an all zero vector of length n
. Iterates are produced until the method either converges, fails, or has computed the maximum number of iterations. Convergence is achieved when an iterate x
has relative residual norm(b-A*x)/norm(b)
less than or equal to the tolerance of the method. The default tolerance is 1e-6
. The default maximum number of iterations is the minimum of n
and 20. No preconditioning is used.
cgs(A,b,tol)
specifies the tolerance of the method, tol
.
cgs(A,b,tol,maxit)
additionally specifies the maximum number of iterations, maxit
.
cgs(A,b,tol,maxit,M) and cgs(A,b,tol,maxit,M1,M2)
use left preconditioner M
or M = M1*M2
and effectively solve the system inv(M)*A*x = inv(M)*b
for x
. If M1
or M2
is given as the empty matrix ([]
), it is considered to be the identity matrix, equivalent to no preconditioning at all. Since systems of equations of the form M*y = r
are solved using backslash within cgs, it is wise to factor preconditioners into their LU factors first. For example, replace cgs(A,b,tol,maxit,M) with:
[M1,M2] = lu(M); cgs(A,b,tol,maxit,M1,M2).
cgs(A,b,tol,maxit,M1,M2,x0)
specifies the initial estimate x0
. If x0
is given as the empty matrix ([]
), the default all zero vector is used.
x = cgs(A,b,tol,maxit,M1,M2,x0)
returns a solution x
. If cgs
converged, a message to that effect is displayed. If cgs
failed to converge after the maximum number of iterations or halted for any reason, a warning message is printed displaying the relative residual norm(b-A*x)/norm(b)
and the iteration number at which the method stopped or failed.
[x,flag] = cgs(A,b,tol,maxit,M1,M2,x0)
returns a solution x
and a flag that describes the convergence of cgs
: flag
is not 0
, the solution x
returned is that with minimal norm residual computed over all the iterations. No messages are displayed if the flag
output is specified.
[x,flag,relres] = cgs(A,b,tol,maxit,M1,M2,x0)
also returns the relative residual norm(b-A*x)/norm(b)
. If flag
is 0
, then relres tol
.
[x,flag,relres,iter] = cgs(A,b,tol,maxit,M1,M2,x0)
also returns the iteration number at which x
was computed. This always satisfies
0
iter
maxit
.
[x,flag,relres,iter,resvec] = cgs(A,b,tol,maxit,M1,M2,x0)
also returns a vector of the residual norms at each iteration, starting from esvec(1) = norm(b-A*x0)
. If flag
is 0
, resvec
is of length iter+1
and resvec(end)
tol*norm(b)
.
load west0479 A = west0479 b = sum(A,2) [x,flag] = cgs(A,b)
flag
is 1
since cgs
will not converge to the default tolerance 1e-6
within the default 20 iterations.
[L1,U1] = luinc(A,1e-5) [x1,flag1] = cgs(A,b,1e-6,20,L1,U1)
flag1
is 2
since the upper triangular U1
has a zero on its diagonal so cgs
fails in the first iteration when it tries to solve a system such as U1*y = r
for y
with backslash.
[L2,U2] = luinc(A,1e-6) [x2,flag2,relres2,iter2,resvec2] = cgs(A,b,1e-15,10,L2,U2)
flag2
is 0
since cgs
will converge to the tolerance of 7.9e-16
(the value of relres2
) at the fifth iteration (the value of iter2
) when preconditioned by the incomplete LU factorization with a drop tolerance of 1e-6
. resvec2(1) = norm(b)
and resvec2(6) = norm(b-A*x2)
. You may follow the progress of cgs
by plotting the relative residuals at each iteration starting from the initial estimate (iterate number 0) with semilogy(0:iter2,res2/norm(b),'-o')
.bicg
BiConjugate Gradients method
bicgstab
BiConjugate Gradients Stabilized method
gmres
Generalized Minimum Residual method (with restarts)
luinc
Incomplete LU matrix factorizations
pcg
Preconditioned Conjugate Gradients method
qmr
Quasi-Minimal Residual method
\
Matrix left division