SHARE
TWEET

gauss elementation

skashminzim Sep 15th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % Code from "Gauss elimination and Gauss Jordan methods using MATLAB"
  2. % https://www.youtube.com/watch?v=kMApKEKisKE
  3.  
  4. a = [3 4 -2 2 2
  5.     4 9 -3 5 8
  6.     -2 -3 7 6 10
  7.     1 4 6 7 2];
  8.  
  9.  
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. %Gauss elimination method [m,n)=size(a);
  12. [m,n]=size(a);%m=num od rows & n=num of column
  13. for j=1:m-1
  14.     for z=2:m
  15.         if a(j,j)==0
  16.             t=a(j,:);
  17.             a(j,:)=a(z,:);
  18.             a(z,:)=t;
  19.         end
  20.     end
  21.     for i=j+1:m
  22.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  23.     end
  24. end
  25. x=zeros(1,m);
  26. for s=m:-1:1
  27.     c=0;
  28.     for k=2:m
  29.         c=c+a(s,k)*x(k);
  30.     end
  31.     x(s)=(a(s,n)-c)/a(s,s);
  32. end
  33. disp('Gauss elimination method:');
  34. a
  35. x'
  36. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  37. % Gauss-Jordan method
  38. [m,n]=size(a);
  39. for j=1:m-1
  40.     for z=2:m
  41.         if a(j,j)==0
  42.             t=a(1,:);a(1,:)=a(z,:);
  43.             a(z,:)=t;
  44.         end
  45.     end
  46.     for i=j+1:m
  47.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  48.     end
  49. end
  50.  
  51. for j=m:-1:2
  52.     for i=j-1:-1:1
  53.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  54.     end
  55. end
  56.  
  57. for s=1:m
  58.     a(s,:)=a(s,:)/a(s,s);
  59.     x(s)=a(s,n);
  60. end
  61. disp('Gauss-Jordan method:');
  62. a
  63. x'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top