數值分析Jacobian迭代

Ann_david發表於2020-11-01
function [X,Number_of_iteration]=Jacobian_iteration(A,B,P,delta,max1)
% Input  - A is an N x N nonsingular matrix
%        - B is an N x 1 matrix
%        - P is an N x 1 matrix: the initial guess
%        - delta is the tolerance for P
%        - max1 is the maximum number of iterations
% Output - X is an N x 1 matrix: the Jacobian approximation to the 
%        - solution of the AX = B
N = length(B);
count = 0;
for k = 1 : max1
    for i = 1 : N
        X(i) = (B(i) - A(i,[1:i-1, i+1:N]) * P([1:i-1, i+1:N]))/A(i,i);
    end
    err = abs(norm(X' - P));
    reletive_err = err / (norm(X) + eps);
    P = X';
    count = count + 1;
        if(err < delta) || (reletive_err < delta)
            break
        end
end
X = X';
Number_of_iteration = count;

 

相關文章