📄 my_cocg.asv
字号:
function [x,iter,flg,res]=my_cocg(A,b,tol,maxit,M1,D,M2,x0)
% [x,iter,flg,res]=my_cocg(A,b,tol,maxit,M1,D,M2,x0)
% This code implements cocg algorithm with preconditioners M1 and M2, D if
% necessary
% flg=0 success; flg=1 failed to ;
% 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>8
error('Too many input arguments!')
end
% set up for the PCG method
flg = 1;
x=x0;
v=b-A*x;
tol = tol*norm_b;
p = zeros(n,1);
beta = 0;
w = v;
if existM1
w = M1 \ w;
end
if existD
w = D \ w;
end
if existM2
w = M2 \ w;
end
rho1 = v.'*w;
res(1) = 0;
for iter = 1:maxit
p = w + beta*p;
u = A*p;
mu = u.'*p;
if mu == 0
flg = 2;
return;
end
alf = rho1 / mu;
x = x + alf*p;
v = v - alf*u;
res(iter) = norm(v);
if res(iter) < tol
flg = 0;
return;
end
w = v;
if existM1
w = M1 \ w;
end
if existD
w = D \ w;
end
if existM2
w = M2 \ w;
end
rho2 = v.'*w;
if abs(rho2) < 1e-16
flg = 3;
return;
end
beta = rho2 / rho1;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -