Advertisement
STANAANDREY

gauss elim

Mar 5th, 2023
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.49 KB | None | 0 0
  1. function [A,x] = gauss(A, b)
  2.     n = length(A);
  3.     for j=1:n-1
  4.         if abs(A(j, j)) == 0
  5.             error('0 piv encounterd!')
  6.         end
  7.         for i=j+1:n
  8.             mult = A(i, j)/A(j, j);
  9.             for k=j:n
  10.                 A(i, k) = A(i, k) - mult*A(j, k);
  11.             end
  12.             b(i) = b(i) - mult*b(j);
  13.         end
  14.     end
  15.     for i=n:-1:1
  16.         for j=i+1:n
  17.             b(i)=b(i)-A(i, j)*x(j);
  18.         end
  19.         x(i)=b(i)/A(i,i);
  20.     end
  21.     x = x';
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement