cgs
Conjugate gradients squared method
Syntax
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,flag] = cgs(a,b,...)
[x,flag,relres] = cgs(a,b,...)
[x,flag,relres,iter] = cgs(a,b,...)
[x,flag,relres,iter,resvec] = cgs(a,b,...)
描述
x = cgs(A,b)
attempts to solve the system of linear equationsa*x = b
forx
。这n
-经过-n
系数矩阵A
must be square and should be large and sparse. The column vectorb
must have lengthn
。您可以指定A
as a function handle,好玩儿
,这样afun(x)
returns斧头
。
Parameterizing Functionsexplains how to provide additional parameters to the function好玩儿
,以及预处理功能mfun
described below, if necessary.
如果cgs
收敛,显示给该效果的消息。如果cgs
fails to converge after the maximum number of iterations or halts for any reason, a warning message is printed displaying the relative residualnorm(b-a*x)/norm(b)
和the iteration number at which the method stopped or failed.
CGS(A,B,TOL)
指定了托勒ance of the method,托尔
。如果托尔
是[]
, thencgs
uses the default,1e-6
。
CGS(A,B,TOL,MAXIT)
specifies the maximum number of iterations,最大
。如果最大
是[]
thencgs
uses the default,min(n,20)
。
CGS(A,B,TOL,MAXIT,M)
和cgs(A,b,tol,maxit,M1,M2)
use the preconditionerM
或者M = M1*M2
和effectively solve the systemInv(M)*A*X = Inv(M)*B
forx
。如果M
是[]
thencgs
applies no preconditioner.M
可以是函数句柄mfun
such thatmfun(x)
returnsM\x
。
cgs(A,b,tol,maxit,M1,M2,x0)
指定初始猜测x0
。如果x0
是[]
, thencgs
uses the default, an all-zero vector.
[x,flag] = cgs(a,b,...)
returns a solutionx
和a flag that describes the convergence ofcgs
。
旗帜 |
收敛 |
---|---|
|
|
|
|
|
Preconditioner |
|
|
|
One of the scalar quantities calculated during |
Whenever旗帜
不是0
, 解决方案x
返回的是,在所有迭代中计算出最小的规范残差。如果旗帜
指定输出。
[x,flag,relres] = cgs(a,b,...)
还返回相对残差norm(b-a*x)/norm(b)
。如果旗帜
是0
, thenrelres <= tol
。
[x,flag,relres,iter] = cgs(a,b,...)
also returns the iteration number at whichx
被计算在哪里0 <= iter <= maxit
。
[x,flag,relres,iter,resvec] = cgs(a,b,...)
还返回每次迭代中残留规范的向量,包括norm(b-A*x0)
。
例子
使用矩阵输入的CGS
A = gallery('wilk',21); b = sum(A,2); tol = 1e-12; maxit = 15; M1 = diag([10:-1:1 1 1:10]); x = cgs(A,b,tol,maxit,M1);
显示消息
cgs converged at iteration 13 to a solution with relative residual 2.4e-016.
将CGS与功能手柄一起使用
此示例替换矩阵A
在上一个示例中,带有矩阵矢量产品功能的句柄好玩儿
,和预处理M1
with a handle to a backsolve functionmfun
。这example is contained in the filerun_cgs
那
Calls
cgs
with the function handle@afun
as its first argument.Contains
好玩儿
作为嵌套函数,以便所有变量run_cgs
可用于好玩儿
和myfun
。
这following shows the code forrun_cgs
:
function x1 = run_cgs n = 21; b = afun(ones(n,1)); tol = 1e-12; maxit = 15; x1 = cgs(@afun,b,tol,maxit,@mfun); function y = afun(x) y = [0; x(1:n-1)] + ... [((n-1)/2:-1:0)'; (1:(n-1)/2)'].*x + ... [x(2:n); 0]; end function y = mfun(r) y = r ./ [((n-1)/2:-1:1)'; 1; (1:(n-1)/2)']; end end
When you enter
x1 = run_cgs
MATLAB®software returns
cgs converged at iteration 13 to a solution with relative residual 2.4e-016.
将CGS与预处理使用。
此示例演示了使用预处理。
加载West0479
, a real 479-by-479 nonsymmetric sparse matrix.
loadWest0479; A = west0479;
Defineb
so that the true solution is a vector of all ones.
b = full(sum(a,2));
设置公差和最大迭代次数。
托尔= 1e-12; maxit = 20;
采用cgs
以所需的公差和迭代次数找到解决方案。
[x0,fl0,rr0,it0,rv0] = cgs(a,b,tol,maxit);
fl0
是1 becausecgs
不收敛到请求的公差1e-12
在请求的20次迭代中。实际上,行为cgs
是so poor that the initial guess (x0 =零(size(a,2),1)
是the best solution and is returned as indicated byIT0 = 0
。MATLAB stores the residual history inrv0
。
Plot the behavior ofcgs
。
semilogy(0:maxit,rv0/norm(b),'-o');xlabel('迭代编号');ylabel(“相对残留”);
该图表明该解决方案不收敛。您可以使用预处理来改善结果。
用伊卢
, sinceA
是nonsymmetric.
[L,U] = ilu(A,struct('类型','ilutp','Droptol',1E-5));
Error using ilu There is a pivot equal to zero. Consider decreasing the drop tolerance or consider using the 'udiag' option.
MATLAB无法构建不完整的LU,因为它会导致一个奇异的因素,这是无用的预处理。
如错误消息所示,您可以使用降低的降低公差重试。
[L,U] = ilu(A,struct('类型','ilutp','Droptol',1e-6)); [x1,fl1,rr1,it1,rv1] = cgs(A,b,tol,maxit,L,U);
fl1
是0 becausecgs
drives the relative residual to4.3851E-014
(the value ofRR1
). The relative residual is less than the prescribed tolerance of1e-12
at the third iteration (the value ofit1
) when preconditioned by the incomplete LU factorization with a drop tolerance of1e-6
。输出RV1(1)
是norm(b)
和the outputRV1(14)
是norm(b-A*x2)
。
You can follow the progress ofcgs
通过从初始估计开始(迭代号0)开始在每次迭代处绘制相对残差。
半学(0:IT1,RV1/NORM(B),,'-o');xlabel('迭代编号');ylabel(“相对残留”);
参考
[1] Barrett, R., M. Berry, T. F. Chan, et al.,Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods,暹罗,费城,1994年。
[2] Sonneveld,Peter,“ CGS:非对称线性系统的快速兰科斯型求解器”,”SIAM J. Sci. Stat. Comput., January 1989, Vol. 10, No. 1, pp. 36–52.