📄 my_cocr_c.asv
字号:
function [x,iter,flg,res]=my_cocr_c(A,b,tol,maxit,M1,D,M2,x0)
% [x,iter,flg,res]=my_cocr(A,b,tol,maxit,M1,D,M2,x0)
% This code implements cocr algorithm with preconditioners M1 and M2, D if
% necessary
% flg=0 success; flg=1 failed to convergent whin maxit iterations;
% flg = 2 singular T; flg = 3 an isotropic basis vector encountered.
% Developed by: Guanghui Cheng UESTC China
% Date : 2008-08-23
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 COCR 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
res(1) = 0;
u = A*w;
Ap = A*p;
for iter = 1:maxit
p = w + beta*p;
Ap = u + beta*Ap;
MAp = Ap;
if existM1
MAp = M1 \ MAp;
end
if existD
MAp = D \ MAp;
end
if existM2
MAp = M2 \ MAp;
end
alf = (w.'*u) / (Ap.'*MAp);
x = x + alf*p;
v = v - alf*Ap;
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
u = A*w;
beta = -(u.'*MAp)/ (Ap.'*MAp);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -