Advertisement
ruhan008

gauss elimination

Nov 1st, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.62 KB | None | 0 0
  1. function x = gauss_elimination(A, B)
  2.     [n, m] = size(A);
  3.  
  4.     if n ~= m
  5.         error('Not a square matrix');
  6.     end
  7.    
  8.     for i = 1 : n - 1
  9.         for j = i + 1 : n
  10.             for k = i + 1 : n
  11.                 A(j, k) = A(j, k) - (A(j, i) / A(i, i)) * A(i, k);
  12.             end
  13.  
  14.             B(j, 1) = B(j, 1) - (A(j, i) / A(i, i)) * B(i, 1);
  15.         end
  16.     end
  17.  
  18.     x = zeros(1, n);
  19.     x(1, n) = B(n) / A(n, n);
  20.  
  21.     for i = n - 1 : -1 : 1
  22.         x(1, i) = B(i, 1);
  23.         for j = i + 1 : n
  24.             x(1, i) = x(1, i) - A(i, j) * x(1, j);
  25.         end
  26.         x(1, i) = x(1, i) / A(i, i);
  27.     end
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement