Documentation

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 = bforx。这n-经过-n系数矩阵Amust be square and should be large and sparse. The column vectorbmust have lengthn。您可以指定Aas a function handle,好玩儿,这样afun(x)returns斧头

Parameterizing Functionsexplains how to provide additional parameters to the function好玩儿,以及预处理功能mfundescribed below, if necessary.

如果cgs收敛,显示给该效果的消息。如果cgsfails 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,托尔。如果托尔[], thencgsuses the default,1e-6

CGS(A,B,TOL,MAXIT)specifies the maximum number of iterations,最大。如果最大[]thencgsuses 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)*Bforx。如果M[]thencgsapplies no preconditioner.M可以是函数句柄mfunsuch thatmfun(x)returnsM\x

cgs(A,b,tol,maxit,M1,M2,x0)指定初始猜测x0。如果x0[], thencgsuses the default, an all-zero vector.

[x,flag] = cgs(a,b,...)returns a solutionx和a flag that describes the convergence ofcgs

旗帜

收敛

0

cgsconverged to the desired tolerance托尔之内最大iterations.

1

cgs迭代最大times but did not converge.

2

PreconditionerMwas ill-conditioned.

3

cgsstagnated. (Two consecutive iterates were the same.)

4

One of the scalar quantities calculated duringcgs变得太小或太大而无法继续计算。

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在上一个示例中,带有矩阵矢量产品功能的句柄好玩儿,和预处理M1with a handle to a backsolve functionmfun。这example is contained in the filerun_cgs

  • Callscgswith the function handle@afunas 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;

Definebso 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 becausecgsdrives the relative residual to4.3851E-014(the value ofRR1). The relative residual is less than the prescribed tolerance of1e-12at 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.

Extended Capabilities

在R2006a之前引入