simonli2575

Gaussian Elimination: Matlab

Jan 29th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.57 KB | None | 0 0
  1. function [x] = gausselim(a,b)
  2. %GAUSSELIM a on the left, b on the right.
  3. a = [a b];
  4. for k = 1:length(a)-2 %Column from 1 to third last
  5.     for i = k+1:length(a)-1 %row from k+1 to second last
  6.         r = a(i,k)/a(k,k);
  7.         for j = k:length(a) %column from k to last, subtraction
  8.             if a(i,j)
  9.                 a(i,j) = a(i,j) - a(k,j)*r;
  10.             end
  11.         end
  12.     end
  13. end
  14. %Solving phase
  15. x = zeros(length(a)-1,1);
  16. for i = length(a)-1:-1:1
  17.     for j = i:length(a)-2
  18.         a(i,end) = a(i,end) - a(i,j+1)*x(j+1);
  19.     end
  20.     x(i) = a(i,end)/a(i,i);
  21. end
Add Comment
Please, Sign In to add comment