Iterative solution of linear equations - Jacobi iteration method

  1. Code

%% Jacobi iteration method (iterative method for the solution of this undesirable pathological matrix) 
%% linear equations M * X = b, M is a square matrix, X0 is the initial solution vector, epsilon is the control accuracy 
function JIM = Jacobian_iteration_method (M , B, X0, Epsilon) 
[m, n-] = size (m); 
D = diag (m); L = zeros (m, n-); the U-= zeros (m, n-); D = zeros (m, n- ); 
Delta = 0; UB = 100; X-= zeros (m, UB); X-(:,. 1) = X0; of X_DELTA in = X-; x_end = zeros (m,. 1); k_end = 0; K =. 1; E Floor = (ABS (log (Epsilon))); 
for I =. 1:. 1: m 
    for J =. 1:. 1: n- 
        IF I> J 
            L (I, J) = -M (I, J); 
        ELSEIF I < J 
            the U-(I, J) = -M (I, J); 
        ELSEIF J == I 
            D (I, J) = D (I); 
        End      
    End 
End 
B = INV (D) * (the U-L +); 
F INV = (D) * B; 
X_real = INV (M) * B;
K =. 1 for:. 1: UB 
    of X_DELTA in (:, K) = X-(:, K) -X_real; 
    Delta = NORM (of X_DELTA in (:, K), 2); 
    IF Delta <Epsilon 
         BREAK 
    End 
    X-(:, K + . 1) = B * X-(:, K) + F; 
End 
DISP ( 'iteration number:'); 
K 
DISP ( 'iterative solution is:'); 
JIM = VPA ([X-(:, K)], E ); 
End

  2. Examples

clear all
clc
for i = 1:8
    for j = 1:8
        if i == j
            M(i,j) = 2.1;
        elseif i - j == 1
            M(i,j) = 1;
        elseif j - i == 1
            M(i,j) = -1;
        else
            M(i,j) = 0;
        end
    end
end
b = [1 2 3 4 4 3 2 1]';
X0 = [1 1 1 1 1 1 1 1]';
epsilon = 1e-4;

S = Jacobian_iteration_method(M,b,X0,epsilon)
M\b

  Results

Iteration number: 
K = 
    73 is 
the iterative solution is: 
S = 
  1.07162282 
  1.25046562 
  1.69755831 
  1.81519199 
  1.50952631 
 0.985363364 
  .57873237 
 0.200592913 
ANS = 
    1.0716 
    1.2504 
    1.6975 
    1.8152 
    1.5096 
    .9853 
    .5787 
    0.2006

  

Guess you like

Origin www.cnblogs.com/guliangt/p/12119352.html