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

📄 my_pcg.asv

📁 求解线性系统的Krylov方法的工具箱
💻 ASV
字号:
function [x,iter,flg,operations]=my_pcg(A,b,tol,maxit,M1,D,M2,x0,ptype)
% [x,iter,relres]=my_pcg(a,b,tol,maxit,M1,D,M2,x0,ptype)
% This code implements CG algorithm with preconditioners M1 and M2, D if
% necessary
% flg=0 success; flg=1 fail;

% Developed by: Plum_Liliang UESTC China
% Date        : 2006-05-13
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 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
nz=nnz(A);
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;
    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 < 9 | isempty(ptype)
    ptype = 0;
end
    
if nargin> 9
    error('Too many input arguments!')
end

% set up for the PCG method
x=x0;
r=b-A*x;
rho=1;
flg=1;
iter=maxit;
operations=nz;
for k=1:maxit
    % Preconditioning
    if ptype == 0 % implicit pre
        if existM1
            y=M1\r;
        else
            y=r;
        end
        if existD
            y=D\y;
        end
        if existM2
            z=M2\y;
        else
            z=y;
        end
    else
        if existM1
            y=M1\r;
        else
            y=r;
        end
        if existD
            y=D\y;
        end
        if existM2
            z=M2\y;
        else
            z=y;
        end
    end
    
    rho1=rho;
    rho=r'*z;
    % The search direction
    if k==1
        p=z;
    else
        beta=rho/rho1;
        p=r+beta*p;
    end
    % 
    w=A*p;
    alpha=rho/(p'*w);
    x=x+alpha*p;
    r=r-alpha*w;
    operations=operations+5*n+nz;
    norm_r=norm(r);
    if norm_r<tol
        flg=0;
        iter=k;
        break;
    end
end

⌨️ 快捷键说明

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