Advertisement
encoree1996

gauss

Apr 13th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.51 KB | None | 0 0
  1. function [ A,b,xn] = gauss( A,b )
  2. n = size(A,1);  
  3.  
  4. for col=1:n-1
  5.     for i=1+col:n
  6.         if A(i, col) == 0
  7.             continue
  8.         end
  9.         mul = A(i,col)/A(col,col);
  10.         A(i,:) = A(i,:) - A(col,:)*mul;
  11.         b(i,:) = b(i,:) - b(col,:)*mul;
  12.     end
  13. end
  14.  
  15. xn = zeros(n,1);
  16. xn(n) = b(n,1)/A(n,n);
  17. for i=n-1:-1:1
  18.     sum = 0;
  19.     for xi=1:n
  20.         sum = sum + xn(xi)*A(i,xi);
  21.     end
  22.     if sum == 0
  23.         xn(i) = 0;
  24.     else
  25.         xn(i) = (b(i)-sum)/A(i,i);
  26.     end
  27. end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement