Skip to content

Instantly share code, notes, and snippets.

@esromneb
Last active November 23, 2022 01:50
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esromneb/1d57b1d16d54cde37332 to your computer and use it in GitHub Desktop.
Save esromneb/1d57b1d16d54cde37332 to your computer and use it in GitHub Desktop.
Gauss elimination and Gauss Jordan methods using MATLAB code
% Code from "Gauss elimination and Gauss Jordan methods using MATLAB"
% https://www.youtube.com/watch?v=kMApKEKisKE
a = [3 4 -2 2 2
4 9 -3 5 8
-2 -3 7 6 10
1 4 6 7 2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Gauss elimination method [m,n)=size(a);
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m:-1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
disp('Gauss elimination method:');
a
x'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Gauss-Jordan method
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(1,:);a(1,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
for j=m:-1:2
for i=j-1:-1:1
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
for s=1:m
a(s,:)=a(s,:)/a(s,s);
x(s)=a(s,n);
end
disp('Gauss-Jordan method:');
a
x'
@facekunal
Copy link

what is the difference bw gauss Jordan method and gauss Jordan elimination

@007shivam
Copy link

007shivam commented Sep 8, 2016

in line 14 , it will be
for z=j+1:m
otherwise
for z=2:m
will not work for
a=[2 1 -1 2 5
4 5 -3 6 9
4 2 -2 9 8
4 11 -4 8 2];

@ps-ruby
Copy link

ps-ruby commented Nov 9, 2016

a(j,:) means?

@elhamzaouiaymen
Copy link

Can i get the matlab gui implementation of gauss elimination ?

@amirshokoohi
Copy link

Good job

@mirmanto
Copy link

if your matrix is changed as shown below, does your program work?

a = [3 4 -2 2 2
4 0 -3 5 8
-2 -3 0 6 10
1 4 6 7 2];
thanks

@dmitry1945
Copy link

dmitry1945 commented Dec 20, 2018

Just as functoin:

function [x] = gauss(a,y)
%GAUSS Summary of this function goes here
%   Detailed explanation goes here
[m,n]=size(a);
a(:, n+1) = y;
[m,n]=size(a);
for j=1:m-1
    for z=2:m
        if a(j,j)==0
            t=a(1,:);a(1,:)=a(z,:);
            a(z,:)=t;
        end
    end
    for i=j+1:m
        a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
    end
end

for j=m:-1:2
    for i=j-1:-1:1
        a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
    end
end

for s=1:m
    a(s,:)=a(s,:)/a(s,s);
    x(s)=a(s,n);
end

end

@msnitish
Copy link

msnitish commented Feb 14, 2019

Can we find inverse using this method ? What is the final output which we get ?

@sefaer
Copy link

sefaer commented Mar 16, 2019

good job

@HashemiGitHub
Copy link

if your matrix is changed as shown below, your program does work!

a=[0 0 0 5 ; 4 0 2 5; 1 3 0 2; 3 4 2 0]

y=[16;10;13;-1]
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment