jacobi.m

来自「fit programme for a serials of data」· M 代码 · 共 39 行

M
39
字号
function [X,n]=jacobi(A,b,X0,e,N0)
% input A is an N*N nonsingular matrix
%       b is an N*1 matrix
%       e is the tolerance for iterations
% output X is an N*1 matrix; the jacobi iterations approximation to 
%        the solution of AX=b
%         N0 is the maximum number of iterations
%rou=norm(eig(A));
%if rou>=1
%   disp(sprintf('the process of iteration is diverge'));
%  return;
%end
N=length(b);
X=X0;
Y=ones(N,1);
for k=1:N0
    for i=1:N
       Y(i)=b(i);
       for j=1:N
          if j~=i
             Y(i)=Y(i)-A(i,j)*X(j); 
          end
       end
       Y(i)=Y(i)/A(i,i);
    end
    error=norm(X-Y);
    if error<e
        n=k;
        X=Y;
        break;
    end
    n=-1;
    X=Y;
end
if n==-1
   disp(sprintf('false')); 
   n=N0;
end

⌨️ 快捷键说明

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