LSQR method
x = lsqr(A,b)
lsqr(A,b,tol)
lsqr(A,b,tol,maxit)
lsqr(A,b,tol,maxit,M)
lsqr(A,b,tol,maxit,M1,M2)
lsqr(A,b,tol,maxit,M1,M2,x0)
[x,flag] = lsqr(A,b,tol,maxit,M1,M2,x0)
[x,flag,relres] = lsqr(A,b,tol,maxit,M1,M2,x0)
[x,flag,relres,iter] = lsqr(A,b,tol,maxit,M1,M2,x0)
[x,flag,relres,iter,resvec] = lsqr(A,b,tol,maxit,M1,M2,x0)
[x,flag,relres,iter,resvec,lsvec] = lsqr(A,b,tol,maxit,M1,M2,x0)
x = lsqr(A,b)
attempts to solve the system of linear equationsA*x=b
forx
ifA
is consistent, otherwise it attempts to solve the least squares solutionx
that minimizesnorm(b-A*x)
. Them
-by-n
coefficient matrixA
need not be square but it should be large and sparse. The column vectorb
must have lengthm
. You can specifyA
as a function handle,afun
, such thatafun(x,'notransp')
returnsA*x
andafun(x,'transp')
returnsA'*x
.
Parameterizing Functionsexplains how to provide additional parameters to the functionafun
, as well as the preconditioner functionmfun
described below, if necessary.
Iflsqr
converges, a message to that effect is displayed. Iflsqr
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.
lsqr(A,b,tol)
specifies the tolerance of the method. Iftol
is[]
, thenlsqr
uses the default,1e-6
.
lsqr(A,b,tol,maxit)
specifies the maximum number of iterations.
lsqr(A,b,tol,maxit,M)
andlsqr(A,b,tol,maxit,M1,M2)
usen
-by-n
preconditionerM
orM = M1*M2
and effectively solve the systemA*inv(M)*y = b
fory
, wherey = M*x
. IfM
is[]
thenlsqr
applies no preconditioner.M
can be a functionmfun
such thatmfun(x,'notransp')
returnsM\x
andmfun(x,'transp')
returnsM'\x
.
lsqr(A,b,tol,maxit,M1,M2,x0)
specifies then
-by-1
initial guess. Ifx0
is[]
, thenlsqr
uses the default, an all zero vector.
[x,flag] = lsqr(A,b,tol,maxit,M1,M2,x0)
also returns a convergence flag.
Flag |
Convergence |
---|---|
0 |
|
1 |
|
2 |
Preconditioner |
3 |
|
4 |
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 you specify theflag
output.
[x,flag,relres] = lsqr(A,b,tol,maxit,M1,M2,x0)
also returns an estimate of the relative residualnorm(b-A*x)/norm(b)
. Ifflag
is0
,relres <= tol
.
[x,flag,relres,iter] = lsqr(A,b,tol,maxit,M1,M2,x0)
also returns the iteration number at whichx
was computed, where0 <= iter <= maxit
.
[x,flag,relres,iter,resvec] = lsqr(A,b,tol,maxit,M1,M2,x0)
also returns a vector of the residual norm estimates at each iteration, includingnorm(b-A*x0)
.
[x,flag,relres,iter,resvec,lsvec] = lsqr(A,b,tol,maxit,M1,M2,x0)
also returns a vector of estimates of the scaled normal equations residual at each iteration:norm((A*inv(M))'*(B-A*X))/norm(A*inv(M),'fro')
. Note that the estimate ofnorm(A*inv(M),'fro')
changes, and hopefully improves, at each iteration.
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 = lsqr(A,b,tol,maxit,M1,M2);
显示the following message:
lsqr converged at iteration 11 to a solution with relative residual 3.5e-009
This example replaces the matrixA
在例1矩阵向量督促处理uct functionafun
. The example is contained in a functionrun_lsqr
that
Callslsqr
with the function handle@afun
as its first argument.
Containsafun
as a nested function, so that all variables inrun_lsqr
are available toafun
.
The following shows the code forrun_lsqr
:
function x1 = run_lsqr 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 = lsqr(@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_lsqr;
MATLAB®software displays the message
lsqr converged at iteration 11 to a solution with relative residual 3.5e-009
[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] Paige, C. C. and M. A. Saunders, "LSQR: An Algorithm for Sparse Linear Equations And Sparse Least Squares," ACM Trans. Math. Soft., Vol.8, 1982, pp. 43-71.