⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_cgs.asv

📁 求解线性系统的Krylov方法的工具箱
💻 ASV
字号:
function [x,iter,flg]=my_CGS(A,b,tol,maxit,M1,D,M2,x0)
% Algorithm: CGS from Templates
% 
% [x,iter,flg]=my_CGS(A,b,tol,maxit,M1,D,M2,x0)
% The input arguments are the usual ones
% flg=0 success; flg=1 fail without convergence; 
% flg=2 fail due to breakdown
% 
% Coding: Plum_LiLiang (SAM of UESTC)
% Date  : 2006-05-29 10:00 pm.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check the dimension of A and the right-hand vector b
[m,n]=size(A);
if m~=n
    error('The coefficient matrix must be square!')
end
[m_b,n_b]=size(b);
if n_b~=1
    error('b must be a vector!')
end

if m~=m_b
    error('The right-hand vecotor must have the same length of A!')
end

% Check the input arguments and assign the default value
if nargin<2
    error('Not enough input arguments!');
end

if (nargin < 3) | isempty(tol)
   tol = 1e-6;
end
if (nargin < 4) | isempty(maxit)
   maxit = min(n,20);
end

% if 'b' is zero then the solution is zero
norm_b=norm(b);
if norm_b==0
    x=0;
    iter=0;
    x=0;
    return;
end

% Check the preconditioners
if nargin>=5 & ~isempty(M1)
    existM1=1;
    if ~isequal(size(M1),[m,n])
        error('The preconditioners M1 should match the size of A!')
    end
else
    existM1=0;
end
if nargin>=6 & ~isempty(D)
    existD=1;
    if ~isequal(size(D),[m,n])
        error('The preconditioners D should match the size of A!')
    end
else
    existD=0;
end
if nargin>=7 & ~isempty(M2)
    existM2=1;
    if ~isequal(size(M2),[m,n])
        error('The preconditioners M2 should match the size of A!')
    end
else
    existM2=0;
end

% Check the initial guess x0
if nargin==8 & ~isempty(x0)
    if ~isequal(size(x0),[m,1])
        error('The initial guess x0 must have the same length of A')
    end
else
    x0=zeros(m,1);
end

if nargin>8
    error('Too many input arguments!')
end

% The main process of iteration (BiCG)
% The set up
x=x0;
r=b-A*x0;
flg=1;
iter=maxit;
r_T=r;
for i=1:maxit
    rho1=r_T'*r;
    if rho1==0
        flg=2;
        break;
    end
    if i==1
        u=r;
        p=u;
    else
        beta=rho1/rho;
        u=r+beta*q;
        p=u+beta*(q+beta*p);
    end
    % Preconditioning
    if existM1
        p_T=M1\p;
    else
        p_T=p;
    end
    if existD
        p_T=D\p_T;
    end
    if existM2
        p_T=M2\p_T;
    end
    v_T=A*p_T;
    alpha=rho1/(r_T'*v_T);
    q=u-alpha*v_T;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -