【问题标题】:GMRES method with given rotations in MATLAB在 MATLAB 中具有给定旋转的 GMRES 方法
【发布时间】:2017-12-08 14:31:10
【问题描述】:

我有以下算法实现

function[x,error,iter,flag,vetnorm_r]=gmres_givens(A,x,b,restart,maxit,tol)


% input   A        REAL nonsymmetric positive definite matrix
%         x        REAL initial guess vector
%         b        REAL right hand side vector
%         M        REAL preconditioner matrix
%         restart  INTEGER number of iterations between restarts
%         maxit    INTEGER maximum number of iterations
%         tol      REAL error tolerance
%
% output  x        REAL solution vector
%         error    REAL error norm
%         iter     INTEGER number of iterations performed
%         flag     INTEGER: 0 = solution found to tolerance
%                           1 = no convergence given maxit

iter = 0;          % initialization                               
flag = 0;

bnrm2=norm(b);
if(bnrm2==0), bnrm2=1.0; end

r=b-A*x;
error=norm(r)/bnrm2;

if(error < tol) return, end

[n,n]=size(A);                   % initialize workspace                
m=restart;
V(:,1:m+1)=zeros(n,m+1);
H(1:m+1,1:m)=zeros(m+1,m);
c(1:m)=zeros(m,1);
s(1:m)=zeros(m,1);
e1=zeros(n,1);
e1(1)=1;
vetnorm_r=zeros(m+1,1);

for iter=1:maxit                         % begin iteration         
r=(b-A*x);
V(:,1)=r/norm(r);
g=norm(r)*e1;
vetnorm_r(1)=norm(r);
for i=1:m                                % construct orthonormal system 
w=(A*V(:,i));                            
for k=1:i
H(k,i)=w'*V(:,k);                        % basis using Gram-Schmidt
w=w-H(k,i)*V(:,k);
end
H(i+1,i)=norm(w);
V(:,i+1)=w/H(i+1,i);

for k=1:i-1                                    % apply Givens rotation
temp=c(k)*H(k,i)+s(k)*H(k+1,i);
H(k+1,i)=-s(k)*H(k,i)+c(k)*H(k+1,i);
H(k,i)=temp;
end
[c(i),s(i)]=givens(H(i,i),H(i+1,i));         % form i-th rotation matrix
temp=c(i)*g(i);                              % approximate residual norm
g(i+1)=-s(i)*g(i);        
g(i)=temp;
H(i,i)=c(i)*H(i,i)+s(i)*H(i+1,i);
H(i+1,i)=0;
error=abs(g(i+1))/bnrm2;
vetnorm_r(i+1)=abs(g(i+1));

if (error <= tol)                           % update approximation
y=H(1:i,1:i)\g(1:i);                        % and exit
x=x+V(:,1:i)*y;
break;
end  
end

if(error <= tol), break, end
y=H(1:m,1:m)\g(1:m);                          % update approximation
x=x+V*y;                                      % compute residual
r=b-A*x                                    
g(i+1)=norm(r);
error=g(i+1)/bnrm2;                         % check convergence
if(error <= tol), break, end;
end

if (error>tol) flag=1; end;        

end

where

function [c,s]=givens(a,b)

if(b==0)
c=1;
s=0;
elseif (abs(b) > abs(a)),
temp=a/b;
s=1/sqrt(1+temp^2);
c=temp*s;
else
temp=b/a;
c=1/sqrt(1+temp^2);
s=temp*c;
end

我的问题是在每次迭代(也可能在每次重新启动时)获取一个向量(可能是一个矩阵)vetnorm_r,其中包含残差的所有范数(作为输出)。我不知道如何构建这个向量或矩阵。

% 输入一个 REAL 非对称正定矩阵 % x REAL 初始猜测向量 % b REAL 右手边向量 % M REAL 前置条件矩阵 % restart INTEGER 重启之间的迭代次数 % maxit INTEGER 最大迭代次数 % tol REAL 容错 % % 输出 x REAL 解向量 % error REAL 错误范数 % iter INTEGER 执行的迭代次数 % flag INTEGER: 0 = 找到公差的解决方案 % 1 = 给定 maxit 没有收敛

感谢您的回复

【问题讨论】:

    标签: matlab iteration linear-algebra qr-decomposition


    【解决方案1】:

    您可能不应该复制您不理解的代码。残差是它说检查收敛的地方。

    if(error <= tol), break, end
    y=H(1:m,1:m)\g(1:m);                          % update approximation
    x=x+V*y;                                      % compute residual
    r=b-A*x                                    
    g(i+1)=norm(r);
    error=g(i+1)/bnrm2;                         % check convergence
    if(error <= tol), break, end;
    end
    

    我不确定你要的是什么。您可以编写代码,使其不会因公差而中断,并且只执行和多次迭代,以便预先存储残差向量,这就是我要做的。或者,您可以将剩余向量长度设置为较长,然后在它接近即将中断时添加更多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多