Main Content

gmres

Solve system of linear equations — generalized minimum residual method

Description

example

x= gmres(A,b)attempts to solve the system of linear equationsA*x = bforx使用theGeneralized Minimum Residual Method。什么时候the attempt is successful,gmresdisplays a message to confirm convergence. Ifgmresfails to converge after the maximum number of iterations or halts for any reason, it displays a diagnostic message that includes the relative residualnorm(b-A*x)/norm(b)和the iteration number at which the method stopped. For this syntax,gmresdoes not restart; the maximum number of iterations is最小(尺寸(a,1),10)

example

x= gmres(A,b,restart)restarts the method everyrestartinner iterations。The maximum number of outer iterations is外= min(size(A,1)/restart,10)。The maximum number of total iterations isrestart*outer, sincegmresperformsrestartinner iterations for each outer iteration. Ifrestartissize(A,1)或者[],ngmresdoes not restart and the maximum number of total iterations is最小(尺寸(a,1),10)

example

x= gmres(A,b,restart,tol)指定该方法的公差。默认公差是1e-6

example

x= gmres(A,b,restart,tol,maxit)specifies the maximum number of外iterationssuch that the total number of iterations does not exceedrestart*maxit。Ifmaxitis[]然后gmresuses the default,min(size(A,1)/restart,10)。Ifrestartissize(A,1)或者[],那么总迭代的最大数量是maxit(代替restart*maxit)。gmresdisplays a diagnostic message if it fails to converge within the maximum number of total iterations.

example

x= gmres(A,b,restart,tol,maxit,M)specifies a preconditioner matrixM和computesxby effectively solving the system M - 1 A x = M - 1 b 。Using a preconditioner matrix can improve the numerical properties of the problem and the efficiency of the calculation.

example

x= gmres(A,b,restart,tol,maxit,M1,M2)specifies factors of the preconditioner matrixMsuch thatM = M1*M2

example

x= gmres(A,b,restart,tol,maxit,M1,M2,x0)specifies an initial guess for the solution vectorx。The default is a vector of zeros.

example

[x,国旗] = gmres(___)returns a flag that specifies whether the algorithm successfully converged. When国旗= 0, convergence was successful. You can use this output syntax with any of the previous input argument combinations. When you specify the国旗output,gmresdoes not display any diagnostic messages.

example

[x,国旗,relres] = gmres(___)还返回相对残差norm(M\(b-A*x))/norm(M\b), which includes the preconditioner matrixM。If国旗is0,nrelres <= tol

example

[x,国旗,relres,iter] = gmres(___)also returns the inner and outer iteration numbers at whichxwas computed as a vector[outer inner]。The outer iteration number lies in the range0 <= iter(1)<= maxit和the inner iteration number is in the range0 <= iter(2)<=重新启动

example

[x,国旗,relres,iter,resvec] = gmres(___)also returns a vector of the residual norms at each inner iteration, including the first residualnorm(M\(b-A*x0))。These are the residual norms for the preconditioned system.

Examples

collapse all

Solve a square linear system usinggmreswith default settings, and then adjust the tolerance and number of iterations used in the solution process.

Create a random sparse matrixAwith 50% density and nonzeros on the main diagonal. Also create a random vectorbfor the right-hand side of Ax = b

RNGdefaultA = sprandn(400,400,0.5) + 12*speye(400); b = rand(400,1);

Solve Ax = b 使用gmres。The output display includes the value of the relative residual error b - Ax b

x = gmres(A,b);
gmres stopped at iteration 10 without converging to the desired tolerance 1e-06 because the maximum number of iterations was reached. The iterate returned (number 10) has relative residual 0.35.

By defaultgmresuses 10 iterations and a tolerance of1e-6, 和the algorithm is unable to converge in those 10 iterations for this matrix. Since the residual is still large, it is a good indicator that more iterations (or a preconditioner matrix) are needed. You also can use a larger tolerance to make it easier for the algorithm to converge.

Solve the system again using a tolerance of1e-4和100 iterations.

tol = 1e-4;最大值= 100;x = gmres(a,b,[],tol,maxit);
GMRE在迭代100停止,而没有收敛到所需的公差0.0001,因为达到了最大迭代次数。迭代返回(数字100)具有相对残留的0.0045。

Even with a looser tolerance and more iterations the residual error does not improve enough for convergence. When an iterative algorithm stalls in this manner it is a good indication that a preconditioner matrix is needed. However,gmresalso has an input that controls the number of inner iterations. By specifying a value for the inner iterations,gmresdoes more work per outer iteration.

Solve the system again using arestartvalue of 100 and amaxitvalue of 20. Rather than doing 100 iterations once,gmresperforms 100 iterations between restarts and repeats this 20 times.

restart = 100; maxit = 20; x = gmres(A,b,restart,tol,maxit);
gmres(100) converged at outer iteration 2 (inner iteration 75) to a solution with relative residual 9.3e-05.

In this case specifying a large restart value forgmres使其能够收敛到允许的迭代次数中的解决方案。但是,大的重新启动值可以在Ais also large.

Examine the effect of using a preconditioner matrix with non-restartedgmres解决线性系统。

Load west0479, a real 479-by-479 nonsymmetric sparse matrix.

loadwest0479A = west0479;

Definebso that the true solution to Ax = b is a vector of all ones.

b = sum(a,2);

Set the tolerance and maximum number of iterations.

TOL = 1E-12;maxit = 20;

Usegmresto find a solution at the requested tolerance and number of iterations. Specify five outputs to return information about the solution process:

  • xis the computed solution toA*x = b

  • fl0is a flag indicating whether the algorithm converged.

  • RR0is the relative residual of the computed answerx

  • it0is a two-element vector[outer inner]indicating the inner and outer iteration numbers whenxwas computed.

  • rv0is a vector of the residual history for b - Ax

[x,fl0,rr0,it0,rv0] = gmres(A,b,[],tol,maxit); fl0
fl0 = 1
RR0
RR0= 0.7603
it0
it0 =1×21 20

fl0is 1 becausegmresdoes not converge to the requested tolerance1e-12在请求的20次迭代中。最好的近似解决方案gmres退货是最后一个(如it0(2) = 20)。MATLAB存储剩余历史rv0

To aid with the slow convergence, you can specify a preconditioner matrix. SinceAis nonsymmetric, useiluto generate the preconditioner M = L U 。Specify a drop tolerance to ignore nondiagonal entries with values smaller than1e-6。Solve the preconditioned system M - 1 A x = M - 1 b by specifyingLU作为输入gmres。Note that when you specify a preconditioner,gmrescalculates the residual norm of the preconditioned system for the outputsRR1rv1

[L,U] = ilu(A,struct('type','ilutp','droptol',1e-6)); [x1,fl1,rr1,it1,rv1] = gmres(A,b,[],tol,maxit,L,U); fl1
fl1 = 0
RR1
RR1= 1.3557e-13
it1
it1 =1×21 6

使用ilu预处理产生的相对残留少于规定的公差1e-12at the sixth iteration. The first residualrv1(1)isnorm(U\(L\b)), whereM = L*U。The last residualrv1(end)isnorm(U\(L\(b-A*x1)))

You can follow the progress ofgmresby plotting the relative residuals at each iteration. Plot the residual history of each solution with a line for the specified tolerance.

semilogy(0:长度(rv0) 1, rv0 /规范(b),'-o') 抓住semilogy(0:length(rv1)-1,rv1/norm(U\(L\b)),'-o') yline(tol,'r--');legend('No preconditioner','ILU preconditioner','Tolerance','地点','East')xlabel('Iteration number')ylabel('Relative residual')

Figure contains an axes object. The axes object contains 3 objects of type line, constantline. These objects represent No preconditioner, ILU preconditioner, Tolerance.

Using a preconditioner with restartedgmres

Load west0479, a real 479-by-479 nonsymmetric sparse matrix.

loadwest0479A = west0479;

Definebso that the true solution to Ax = b is a vector of all ones.

b = sum(a,2);

构建一个不完整的LU预处理,具有下降的耐受性1e-6

[L,U] = ilu(A,struct('type','ilutp','droptol',1e-6));

The benefit to using restartedgmresis to limit the amount of memory required to execute the method. Without restart,gmresrequiresmaxitvectors of storage to keep the basis of the Krylov subspace. Also,gmres必须支持hogonalize against all of the previous vectors at each step. Restarting limits the amount of workspace used and the amount of work done per outer iteration.

Execute转基因(3),gmres(4), 和gmres(5)使用the incomplete LU factors as preconditioners. Use a tolerance of1e-12和a maximum of 20 outer iterations.

TOL = 1E-12;maxit = 20;[x3,fl3,rr3,it3,rv3] = gmres(a,b,3,tol,maxit,l,u);[x4,fl4,rr4,it4,rv4] = gmres(a,b,4,tol,maxit,l,u);[x5,fl5,rr5,it5,rv5] = gmres(a,b,5,tol,maxit,l,u);fl3
fl3= 0
fl4
fl4 = 0
fl5
fl5 = 0

fl3,fl4, 和fl5are all 0 because in each case restartedgmresdrives the relative residual to less than the prescribed tolerance of1e-12

The following plot shows the convergence history of each restartedgmresmethod.转基因(3)converges at outer iteration 5, inner iteration 3 (it3 = [5, 3]) which would be the same as outer iteration 6, inner iteration 0, hence the marking of 6 on the final tick mark.

semilogy(1:1/3:6,rv3/norm(U\(L\b)),'-o');h1 = gca; h1.XTick = (1:6); title('gmres(N) for N = 3, 4, 5')xlabel(“外迭代编号”);ylabel('Relative residual');holdsemilogy(1:1/4:3,rv4/norm(U\(L\b)),'-o');semilogy(1:1/5:2.8,rv5/norm(U\(L\b)),'-o');yline(tol,'r--');holdofflegend('gmres(3)','gmres(4)','gmres(5)','Tolerance') grid

Figure contains an axes object. The axes object with title gmres(N) for N = 3, 4, 5 contains 4 objects of type line, constantline. These objects represent gmres(3), gmres(4), gmres(5), Tolerance.

In general the larger the number of inner iterations, the more workgmresdoes per outer iteration and the faster it can converge.

Examine the effect of supplyinggmres最初猜测解决方案。

Create a tridiagonal sparse matrix. Use the sum of each row as the vector for the right-hand side of Ax = b 这样可以预期的解决方案 x is a vector of ones.

n = 900;e =一个(n,1);a = spdiags([E 2*e e],-1:1,n,n);b = sum(a,2);

Usegmresto solve Ax = b twice: one time with the default initial guess, and one time with a good initial guess of the solution. Use 200 iterations and the default tolerance for both solutions. Specify the initial guess in the second solution as a vector with all elements equal to0.99

maxit = 200; x1 = gmres(A,b,[],[],maxit);
gmres converged at iteration 27 to a solution with relative residual 9.5e-07.
x0 = 0.99*e;x2 = gmres(a,b,[],[],maxit,[],[],x0);
gmr聚合在迭代7到一个解决方案relative residual 6.7e-07.

In this case supplying an initial guess enablesgmresto converge more quickly.

返回中间结果

You also can use the initial guess to get intermediate results by callinggmresin a for-loop. Each call to the solver performs a few iterations and stores the calculated solution. Then you use that solution as the initial vector for the next batch of iterations.

For example, this code performs 100 iterations four times and stores the solution vector after each pass in the for-loop:

x0 = zeros(size(A,2),1); tol = 1e-8; maxit = 100;fork = 1:4 [x,flag,relres] = gmres(A,b,[],tol,maxit,[],[],x0); X(:,k) = x; R(k) = relres; x0 = x;end

X(:,k)is the solution vector computed at iterationkof the for-loop, andR(k)is the relative residual of that solution.

Solve a linear system by providinggmreswith a function handle that computesA*xin place of the coefficient matrixA

威尔金森测试矩阵之一galleryis a 21-by-21 tridiagonal matrix. Preview the matrix.

A = gallery('wilk',21)
A =21×21101 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 7 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 6 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 ⋮

The Wilkinson matrix has a special structure, so you can represent the operationA*x带有功能手柄。什么时候Amultiplies a vector, most of the elements in the resulting vector are zeros. The nonzero elements in the result correspond with the nonzero tridiagonal elements ofA。Moreover, only the main diagonal has nonzeros that are not equal to 1.

The expression Ax becomes:

Ax = [ 10 1 0 0 0 1 9 1 0 0 0 1 8 1 0 0 1 7 1 0 0 1 6 1 0 0 1 5 1 0 0 1 4 1 0 0 1 3 0 0 0 1 0 0 0 1 10 ] [ x 1 x 2 x 3 x 4 x 5 x 21 ] = [ 10 x 1 + x 2 x 1 + 9 x 2 + x 3 x 2 + 8 x 3 + x 4 x 19 + 9 x 20. + x 21 x 20. + 10 x 21 ]

The resulting vector can be written as the sum of three vectors:

Ax = [ 0 + 10 x 1 + x 2 x 1 + 9 x 2 + x 3 x 2 + 8 x 3 + x 4 x 19 + 9 x 20. + x 21 x 20. + 10 x 21 + 0 ] = [ 0 x 1 x 20. ] + [ 10 x 1 9 x 2 10 x 21 ] + [ x 2 x 21 0 ]

In MATLAB®, write a function that creates these vectors and adds them together, thus giving the value ofA*x:

功能y = afun(x) y = [0; x(1:20)] +。。。[(10:-1:0)'; (1:10)'].*x +。。。[x(2:21); 0];end

(This function is saved as a local function at the end of the example.)

Now, solve the linear system Ax = b by providinggmreswith the function handle that calculatesA*x。Use a tolerance of1e-12, 15 outer iterations, and 10 inner iterations before restart.

B =一个(21,1);TOL = 1E-12;最大值= 15;重新启动= 10;x1 = gmres(@afun,b,ristart,tol,maxit)
gmres(10) converged at outer iteration 5 (inner iteration 10) to a solution with relative residual 5.3e-13.
x1 =21×10.0910 0.0899 0.0999 0.1109 0.1241 0.1443 0.1544 0。2383 0.1309 0.5000 ⋮

检查一下afun(x1)产生一个矢量。

afun(x1)
ans =21×11.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 ⋮

Local Functions

功能y = afun(x) y = [0; x(1:20)] +。。。[(10:-1:0)'; (1:10)'].*x +。。。[x(2:21); 0];end

Input Arguments

collapse all

系数矩阵,指定为方形矩阵或函数手柄。该矩阵是线性系统中的系数矩阵A*x = b。一般来说,A是一个较大的稀疏矩阵或一个函数手柄,它返回了大型稀疏矩阵和列矢量的乘积。

SpecifyingAas a Function Handle

您可以选择将系数矩阵指定为函数句柄而不是矩阵。该函数手柄返回矩阵矢量产物,而不是形成整个系数矩阵,从而使计算更有效。下载188bet金宝搏

要使用函数句柄,请使用功能签名功能y = afun(x)Parameterizing Functionsexplains how to provide additional parameters to the functionafun, if necessary. The function callafun(x)must return the value ofA*x

Data Types:double|功能_handle
Complex Number Support:Yes

线性方程的右侧, specified as a column vector. The length ofbmust be equal tosize(A,1)

Data Types:double
Complex Number Support:Yes

Number of inner iterations before restart, specified as a scalar integer. Use this input along with themaxitinput to control the maximum number of iterations,restart*maxit。Ifrestartis[]或者size(A,1),ngmres不重新启动,迭代的总数为maxit

一个大的restart价值通常会导致更好的收敛行为,但也具有更高的时间和内存要求。

Data Types:double

Method tolerance, specified as a positive scalar. Use this input to trade-off accuracy and runtime in the calculation.gmres必须满足允许迭代次数成功的公差。较小的价值tolmeans the answer must be more precise for the calculation to be successful.

Data Types:double

Maximum number of outer iterations, specified as a positive scalar integer. Increase the value ofmaxitto allow more iterations forgmres满足公差tol。一般来说,the smaller the value oftol,more iterations are required to successfully complete the calculation.

If therestartinput is also specified, then the total number of iterations isrestart*maxit。Otherwise, the total number of iterations ismaxit

The default value ofmaxitdepends on whetherrestartis specified:

  • Ifrestartis unspecified, or specified as[]或者size(A,1),n the default value ofmaxitis最小(尺寸(a,1),10)

  • Ifrestart被指定为范围内的值1 <= restart < size(A,1),n the default value ofmaxitismin(ceil(size(A,1)/restart),10)

Data Types:double

预处理矩阵,指定为矩阵或函数手柄的单独参数。您可以指定预处理矩阵M或者its matrix factorsM = M1*M2to improve the numerical aspects of the linear system and make it easier forgmresto converge quickly. You can use the incomplete matrix factorization functionsiluicholto generate preconditioner matrices. You also can useequilibrateprior to factorization to improve the condition number of the coefficient matrix. For more information on preconditioners, seeIterative Methods for Linear Systems

gmrestreats unspecified preconditioners as identity matrices.

SpecifyingMas a Function Handle

You can optionally specify any ofM,M1, orM2as function handles instead of matrices. The function handle performs matrix-vector operations instead of forming the entire preconditioner matrix, making the calculation more efficient.

要使用函数句柄,请使用功能签名功能y = mfun(x)Parameterizing Functionsexplains how to provide additional parameters to the functionmfun, if necessary. The function callmfun(x)must return the value ofM\x或者M2\(M1\x)

Data Types:double|功能_handle
Complex Number Support:Yes

Initial guess, specified as a column vector with length equal to尺寸(A,2)。If you can providegmreswith a more reasonable initial guessx0than the default vector of zeros, then it can save computation time and help the algorithm converge faster.

Data Types:double
Complex Number Support:Yes

Output Arguments

collapse all

Linear system solution, returned as a column vector. This output gives the approximate solution to the linear systemA*x = b。If the calculation is successful (国旗= 0), thenrelresis less than or equal totol

什么时候ever the calculation is not successful (国旗~= 0), the solutionxreturned bygmresis the one with minimal residual norm computed over all the iterations.

收敛标志,作为该表中的标量值之一返回。收敛标志指示计算是否成功,并区分了几种不同形式的故障。

Flag Value

Convergence

0

Success —gmresconverged to the desired tolerancetolwithinmaxit迭代。

1

Failure —gmresiteratedmaxititerations but did not converge.

2

失败 - 预处理矩阵M或者M = M1*M2is ill conditioned.

3

Failure —gmresstagnated after two consecutive iterations were the same.

4

失败 - 标量数量之一gmresalgorithm became too small or too large to continue computing.

Relative residual error, returned as a scalar. The relative residual errorrelres = norm(M\(b-A*x))/norm(M\b)is an indication of how accurate the answer is. Note thatgmresincludes the preconditioner matrixMin the relative residual calculation, while most other iterative solvers do not. If the calculation converges to the tolerancetolwithinmaxititerations, thenrelres <= tol

Data Types:double

Outer and inner iteration numbers, returned as a two-element vector[outer inner]。该输出指示内部和外迭代编号,计算出的答案xwas calculated:

  • Ifrestartis unspecified, or specified as[]或者size(A,1),n外= 1和all iterations are considered to be inner iterations.

  • Ifrestart被指定为范围内的值1 <= restart < size(A,1),然后外迭代编号在该范围内0 <= outer <= maxit和the inner iteration number is in the range0 <= inner <= restart

Data Types:double

Residual error, returned as a vector. The residual errornorm(M\(b-A*x))reveals how close the algorithm is to converging for a given value ofx。Note thatgmresincludes the preconditioner matrixMin the relative residual calculation, while most other iterative solvers do not. The number of elements inresvecis equal to the total number of iterations (ifrestartis used, this is at mostrestart*maxit)。You can examine the contents ofresvecto help decide whether to change the values ofrestart,tol, ormaxit

Data Types:double

More About

collapse all

Generalized Minimum Residual Method

The generalized minimum residual (GMRES) algorithm was developed to extend the minimum residual (MINRES) algorithm to unsymmetric matrices.

像共轭梯度(CG)方法一样,GMRES算法计算正交序列,但是GMRE需要将所有先前的向量存储在序列中。如果未选中,先前的向量的存储可能会消耗大量内存。该算法的“重新启动”版本通过定期清除中间序列并将结果用作另一个迭代中的初始值来控制这些序列的存储。

Choosing an appropriate "restart" value is essential to good performance, but choosing such a value is mostly a matter of experience. If the number of iterations before restart is too small, the algorithm might be very slow to converge or fail to converge entirely. But if the restart value is too large, then the algorithm has increased storage requirements and might do unnecessary work[1]

Inner and Outer Iterations

Inner iterationsare the iterations thatgmrescompletes before restarting. You can specify the number of inner iterations with therestart争论。

Each timegmres重新启动,外iterationnumber advances. You can specify the maximum number of outer iterations with themaxit争论。外迭代的默认编号是min(size(A,1)/restart,10)

For example, if you do not specifyrestart,n the maximum number of iterations is determined by the value ofmaxit, 和gmresdoes not restart:

If the restart argument is not specified and the value of maxit is 4, then gmres performs a total of 4 iterations.

However, when you specifyrestart,gmres功能performs several inner iterations (specified byrestart)对于每个外迭代(由maxit)。In this case, the maximum number of total iterations isrestart*maxit:

If the restart argument is specified as 4, and the maxit argument is specified as 2, then gmres performs 4 inner iterations for each outer iteration for a total of 8 iterations.

Tips

  • Convergence of most iterative methods depends on the condition number of the coefficient matrix,cond(A)。You can useequilibrateto improve the condition number ofA, 和上its own this makes it easier for most iterative solvers to converge. However, usingequilibratealso leads to better quality preconditioner matrices when you subsequently factor the equilibrated matrixB = R*P*A*C

  • You can use matrix reordering functions such asdissectsymrcm要计算到系数矩阵的行和列,并在系数矩阵时将非齐射码的数量最小化以生成预处理。这可以减少随后求解预处理的线性系统所需的内存和时间。

References

[1] Barrett, R., M. Berry, T. F. Chan, et al.,Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods, SIAM, Philadelphia, 1994.

[2] Saad, Yousef and Martin H. Schultz, “GMRES: A generalized minimal residual algorithm for solving nonsymmetric linear systems,”暹罗j .科学。Stat。第一版。,1986年7月,第1卷。7,第3号,第856-869页。

Extended Capabilities

在R2006a之前引入