qmr
Quasi-minimal residual method
Syntax
x = qmr(A,b)
qmr(A,b,tol)
qmr(A,b,tol,maxit)
qmr(A,b,tol,maxit,M)
qmr(A,b,tol,maxit,M1,M2)
qmr(A,b,tol,maxit,M1,M2,x0)
[x,flag] = qmr(A,b,...)
[x,flag,relres] = qmr(A,b,...)
[x,flag,relres,iter] = qmr(A,b,...)
[x,flag,relres,iter,resvec] = qmr(A,b,...)
Description
x = qmr(A,b)
attempts to solve the system of linear equationsA*x=b
forx
. Then
-by-n
coefficient matrixA
must be square and should be large and sparse. The column vectorb
must have lengthn
. You can specifyA
as a function handle,afun
, such thatafun(x,'notransp')
returnsA*x
andafun(x,'transp')
returns‘* x
.
Parameterizing Functionsexplains how to provide additional parameters to the functionafun
, as well as the preconditioner functionmfun
described below, if necessary.
Ifqmr
converges, a message to that effect is displayed. Ifqmr
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)
and the iteration number at which the method stopped or failed.
qmr(A,b,tol)
specifies the tolerance of the method. Iftol
is[]
, thenqmr
uses the default,1e-6
.
qmr(A,b,tol,maxit)
specifies the maximum number of iterations. Ifmaxit
is[]
, thenqmr
uses the default,min(n,20)
.
qmr(A,b,tol,maxit,M)
andqmr(A,b,tol,maxit,M1,M2)
use preconditionersM
orM = M1*M2
and effectively solve the systeminv(M)*A*x = inv(M)*b
forx
. IfM
is[]
thenqmr
applies no preconditioner.M
can be a function handlemfun
such thatmfun(x,'notransp')
returnsM\x
andmfun(x,'transp')
returnsM'\x
.
qmr(A,b,tol,maxit,M1,M2,x0)
specifies the initial guess. Ifx0
is[]
, thenqmr
uses the default, an all zero vector.
[x,flag] = qmr(A,b,...)
also returns a convergence flag.
Flag |
Convergence |
---|---|
|
|
|
|
|
Preconditioner |
|
The method stagnated. (Two consecutive iterates were the same.) |
|
One of the scalar quantities calculated during |
Wheneverflag
is not0
, the solutionx
returned is that with minimal norm residual computed over all the iterations. No messages are displayed if theflag
output is specified.
[x,flag,relres] = qmr(A,b,...)
also returns the relative residualnorm(b-A*x)/norm(b)
. Ifflag
is0
,relres <= tol
.
[x,flag,relres,iter] = qmr(A,b,...)
also returns the iteration number at whichx
was computed, where0 <= iter <= maxit
.
[x,flag,relres,iter,resvec] = qmr(A,b,...)
also returns a vector of the residual norms at each iteration, includingnorm(b-A*x0)
.
Examples
Using qmr with a Matrix Input
This example shows how to useqmr
with a matrix input. The code:
n = 100; on = ones(n,1); A = spdiags([-2*on 4*on -on],-1:1,n,n); b = sum(A,2); tol = 1e-8; maxit = 15; M1 = spdiags([on/(-2) on],-1:0,n,n); M2 = spdiags([4*on -on],0:1,n,n); x = qmr(A,b,tol,maxit,M1,M2);
displays the message:
qmr converged at iteration 9 to a solution... with relative residual 5.6e-009
Using qmr with a Function Handle
This example replaces the matrixA
in the previous example with a handle to a matrix-vector product functionafun
. The example is contained in a filerun_qmr
that
Calls
qmr
with the function handle@afun
as its first argument.Contains
afun
as a nested function, so that all variables inrun_qmr
are available toafun
.
The following shows the code forrun_qmr
:
function x1 = run_qmr n = 100; on = ones(n,1); A = spdiags([-2*on 4*on -on],-1:1,n,n); b = sum(A,2); tol = 1e-8; maxit = 15; M1 = spdiags([on/(-2) on],-1:0,n,n); M2 = spdiags([4*on -on],0:1,n,n); x1 = qmr(@afun,b,tol,maxit,M1,M2); function y = afun(x,transp_flag) if strcmp(transp_flag,'transp') % y = A'*x y = 4 * x; y(1:n-1) = y(1:n-1) - 2 * x(2:n); y(2:n) = y(2:n) - x(1:n-1); elseif strcmp(transp_flag,'notransp') % y = A*x y = 4 * x; y(2:n) = y(2:n) - 2 * x(1:n-1); y(1:n-1) = y(1:n-1) - x(2:n); end end end
When you enter
x1=run_qmr;
MATLAB®software displays the message
qmr converged at iteration 9 to a solution with relative residual 5.6e-009
Using qmr with a Preconditioner
This example demonstrates the use of a preconditioner.
LoadA = 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));
Set the tolerance and maximum number of iterations.
tol = 1e-12; maxit = 20;
Useqmr
在请求的宽容和找到解决办法number of iterations.
[x0,fl0,rr0,it0,rv0] = qmr(A,b,tol,maxit);
fl0
is 1 becauseqmr
does not converge to the requested tolerance1e-12
within the requested 20 iterations. The seventeenth iterate is the best approximate solution and is the one returned as indicated byit0 = 17
. MATLAB stores the residual history inrv0
.
Plot the behavior ofqmr
.
semilogy(0:maxit,rv0/norm(b),'-o'); xlabel('Iteration number'); ylabel('Relative residual');
The plot shows that the solution does not converge. You can use a preconditioner to improve the outcome.
Create the preconditioner withilu
, since the matrixA
是nonsymmetric.
[L,U] = ilu(A,struct('type','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 cannot construct the incomplete LU as it would result in a singular factor, which is useless as a preconditioner.
You can try again with a reduced drop tolerance, as indicated by the error message.
[L,U] = ilu(A,struct('type','ilutp','droptol',1e-6)); [x1,fl1,rr1,it1,rv1] = qmr(A,b,tol,maxit,L,U);
fl1
is 0 becauseqmr
drives the relative residual to4.1410e-014
(the value ofrr1
). The relative residual is less than the prescribed tolerance of1e-12
at the sixth iteration (the value ofit1
) when preconditioned by the incomplete LU factorization with a drop tolerance of1e-6
. The outputrv1(1)
isnorm(b)
, and the outputrv1(7)
isnorm(b-A*x2)
.
You can follow the progress ofqmr
by plotting the relative residuals at each iteration starting from the initial estimate (iterate number 0).
semilogy(0:it1,rv1/norm(b),'-o'); xlabel('Iteration number'); ylabel('Relative residual');
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] Freund, Roland W. and Nöel M. Nachtigal, “QMR: A quasi-minimal residual method for non-Hermitian linear systems,”SIAM Journal: Numer. Math.60, 1991, pp. 315–339.