SHARE
TWEET

GaussJordan

a guest Sep 15th, 2019 89 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);
  13. for j=1:m-1
  14.     for z=2:m
  15.         if a(j,j)==0
  16.             t=a(j,:);a(j,:)=a(z,:);
  17.             a(z,:)=t;
  18.         end
  19.     end
  20.     for i=j+1:m
  21.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  22.     end
  23. end
  24. x=zeros(1,m);
  25. for s=m:-1:1
  26.     c=0;
  27.     for k=2:m
  28.         c=c+a(s,k)*x(k);
  29.     end
  30.     x(s)=(a(s,n)-c)/a(s,s);
  31. end
  32. disp('Gauss elimination method:');
  33. a
  34. x'
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. % Gauss-Jordan method
  37. [m,n]=size(a);
  38. for j=1:m-1
  39.     for z=2:m
  40.         if a(j,j)==0
  41.             t=a(1,:);a(1,:)=a(z,:);
  42.             a(z,:)=t;
  43.         end
  44.     end
  45.     for i=j+1:m
  46.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  47.     end
  48. end
  49.  
  50. for j=m:-1:2
  51.     for i=j-1:-1:1
  52.         a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
  53.     end
  54. end
  55.  
  56. for s=1:m
  57.     a(s,:)=a(s,:)/a(s,s);
  58.     x(s)=a(s,n);
  59. end
  60. disp('Gauss-Jordan method:');
  61. a
  62. 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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top