gaosai.m

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

M
36
字号
function [X,n]=gaosai(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
%       X0 is an N*1 matrix,the initial guess of AX=b
% 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;
for n=1:N0
    error=0;
    for i=1:N
        t=X(i);
%        X(i)=b(i);
%        for j=1:N
%            if j~=i
%               X(i)=X(i)-A(i,j)*X(j); 
%            end
%        end
        X(i)=(b(i)-A(i,1:i-1)*X(1:i-1)-A(i,i+1:N)*X(i+1:N))/A(i,i);
%        X(i)=X(i)/A(i,i);
        if abs(X(i)-t)>error
           error=abs(X(i)-t); 
        end
    end
    if error<e
       return; 
    end
end
disp(sprintf('false'))

⌨️ 快捷键说明

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