Last active
November 23, 2022 01:50
-
-
Save esromneb/1d57b1d16d54cde37332 to your computer and use it in GitHub Desktop.
Gauss elimination and Gauss Jordan methods using MATLAB code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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' | |
Can i get the matlab gui implementation of gauss elimination ?
Good job
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
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
Can we find inverse using this method ? What is the final output which we get ?
good job
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
a(j,:) means?