Advertisement
HaS5HeM

MATLAB JACOBI'S ALGO

May 28th, 2021 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.02 KB | None | 0 0
  1. % Algorithm to find approx solution of unknown variables in a system of
  2. % linear equations using Jacobi's method
  3.  
  4. A = zeros(3, 3);
  5. C = zeros(3, 1);
  6. X = zeros(3, 1);
  7. X1 = 0; X2 = 0; X3 = 0; FLAG = 0;
  8.  
  9. fprintf('Enter the coefficients of x1 one by one,\nthen the coefficients of x2, and so on:\n');
  10. for i = 1:9
  11.     A(i) = input('Enter the element:   ');
  12. end
  13.  
  14. fprintf('\nEnter the vector of constants:\n');
  15. for i = 1:3
  16.     C(i) = input('Enter the element:   ');
  17. end
  18.  
  19. ITR = input('Enter the number of iterations');
  20. TOL = input('Enter the stopping tolerance');
  21.  
  22. for i = 1:ITR
  23.     X(1) = (1 / A(1,1)) * (C(1) - A(1,2) * X2 - A(1,3) * X3);
  24.     X(2) = (1 / A(2,2)) * (C(2) - A(2,1) * X1 - A(2,3) * X3);
  25.     X(3) = (1 / A(3,3)) * (C(3) - A(3,1) * X1 - A(3,2) * X2);
  26.     if (abs(X(1) - X1) < TOL) && (abs(X(2) - X2) < TOL) && (abs(X(3) - X3) < TOL)
  27.         disp(round(X(1:3), 3))
  28.         disp(i)
  29.         FLAG = 1;
  30.         break
  31.     end
  32.     X1 = X(1); X2 = X(2); X3 = X(3);
  33. end
  34. if FLAG == 0
  35.     disp(round(X(1:3), 3))
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement