Gaussian Elimination technique by matlab

Hello every body , i am trying to solve an (nxn) system equations by Gaussian Elimination method using Matlab , for example the system below :
x1 + 2x2 - x3 = 3
2x1 + x2 - 2x3 = 3
-3x1 + x2 + x3 = -6
C = [ 1 2 -1 ; 2 1 -2 ; -3 1 1 ]
b= [ 3 3 -6 ]
by using this code :
% Matlab Program to solve (nxn) system equation
% by using Gaussian Elimination method
clear ; clc ; close all
n = input('Please Enter the size of the equation system n = ') ;
C = input('Please Enter the elements of the Matrix C ' ) ;
b = input('Please Enter the elements of the Matrix b ' ) ;
dett = det(C)
if dett == 0
print('This system unsolvable because det(C) = 0 ')
else
b = b'
A = [ C b ]
for j = 1:(n-1)
for i= (j+1) : n
mult = A(i,j)/A(j,j) ;
for k= j:n+1
A(i,k) = A(i,k) - mult*A(j,k) ;
A
end
end
end
for p = n:-1:1
for r = p+1:n
x(p) = A(p,r)/A(p,r-1)
end
end
end
everything is good but i need help to do the back substitution to find and print the matrix x ( which is contains the solutions of this system ) , could any one help me to do that ? i will thankful in advance Razi

댓글 수: 9

John D'Errico
John D'Errico 2017년 5월 14일
편집: John D'Errico 2017년 5월 14일
Note that testing to see if det(C)==0 is a terribly bad idea, since it will essentially NEVER be zero, even for a singular matrix. Yes, I know they taught you that in class. But "they" are wrong here.
For example:
A = rand(3,4);
A = [A;A(1,:)];
det(A)
ans =
-1.5101e-19
Here A has an EXACT duplicate row. Is det(A)==0?
How about this one, here A has rank 2, in a 10x10 matrix.
A = randn(10,2)*randn(2,10);
det(A)
ans =
-5.4818e-125
Not zero. How about this one?
A = randn(10,9)*rand(9,10)*100;
det(A)
ans =
7.1118e+06
Still not zero. In fact, this one had a pretty large determinant for a known to be singular matrix. So you cannot even test to see if det is a small number, since it can easily be quite large yet the matrix is still singular.
rank(A)
ans =
9
Use tools like rank or cond to decide if a matrix is singular. NOT det. Anyone who tells you to use det using floating point arithmetic is flat out wrong.
det is a liar.
Razi Naji
Razi Naji 2017년 5월 15일
Ok Mr. John , thanks for your info and your advice , i will try to rewrite the code again with another form . Best regards
I don't understand that how much amounts of size in the equation system n?So please give me answer.
Robby Ching
Robby Ching 2020년 5월 26일
Hello! I have question. What does n:-1:1 mean? Thank you
AKSHAY KUMAR
AKSHAY KUMAR 2020년 11월 2일
if n=3, then for p = 3:-1:1 will generate 3,2,1.
It simply means that p will go from 3 to 1 decreasing by 1
Robby Ching
Robby Ching 2020년 11월 3일
Thank you so much!
jealryn obordo
jealryn obordo 2022년 2월 20일
can I ask , what is the value of n?
Muntasir
Muntasir 2022년 6월 15일
Jealryn obordo
'n' is the number of variables or equations. Here n=3
shrawani
shrawani 2024년 3월 5일
what is A(i,j) and how, why it is used

댓글을 달려면 로그인하십시오.

 채택된 답변

suraj kumar
suraj kumar 2020년 2월 23일

1 개 추천

% Gauss-Elimination method
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
enda = input('Enter the augument matrix:')
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
enda = input('Enter the augument matrix:')
[m,n]=size(a);
0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
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'

댓글 수: 2

Muhammed Ali
Muhammed Ali 2020년 12월 31일
hello sir what does variable m does in this code ?
like what is its purpose explained in your words
DGM
DGM 2023년 5월 18일
편집: DGM 2023년 5월 18일
Besides the missing variables, this isn't working code. The missing linebreaks have broken things in a few spots, and there's nonsense like this:
if a(j,j)==for i=j+1:m
This probably means that there is code that's either missing, or a chunk of the code was selected and moved to an arbitrary position in the body of surrounding code. It's an incomplete jumbled mess.
Why did anyone post this? Why did anyone accept it?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

M Waqar Nadeem
M Waqar Nadeem 2021년 3월 11일

7 개 추천

C = [1 2 -1; 2 1 -2; -3 1 1]
b= [3 3 -6]'
A = [C b]; %Augmented Matrix
n= size(A,1); %number of eqns/variables
x = zeros(n,1); %variable matrix [x1 x2 ... xn] coulmn
for i=1:n-1
for j=i+1:n
m = A(j,i)/A(i,i)
A(j,:) = A(j,:) - m*A(i,:)
end
end
x(n) = A(n,n+1)/A(n,n)
for i=n-1:-1:1
summ = 0
for j=i+1:n
summ = summ + A(i,j)*x(j,:)
x(i,:) = (A(i,n+1) - summ)/A(i,i)
end
end

댓글 수: 3

Big thanks
Ahmad
Ahmad 2022년 4월 30일
Hi Nadeem
seems it does not work for:
C=[0 0 0 5 ; 4 0 2 5; 1 3 0 2; 3 4 2 0]
b=[16 10 13 -1]'
Mourad
Mourad 2022년 10월 30일
it does work only if the elements of the diagonal are different from zero.

댓글을 달려면 로그인하십시오.

pss CHARAN
pss CHARAN 2020년 12월 2일
편집: Walter Roberson 2024년 3월 8일

0 개 추천

clc
n=input(Enter number of variables);
for i=1:1:n
for j=1:1:n
A(i,j)=input(Enter Coefficient);
end
end
for i=1:1:n
for j=1:1:n
B(i,1)=input(Enter Constant);
end
end
A
B
E=[AB]
for i=1:1:n
E(i,:)=E(i,:)+E(i+1,:);
end
E(n,1)=E(n,1)+E(1,1);
q=1;
for i=q:1:n
for j=1:1:n
if E(j,i)==0;
E(j,:)=E(j,:);
else
E(j,:)=E(j,:)/E(j,i);
end
end
for k=i+1:1:n
E(k,:)=E(k,:)-E(i,:);
end
q=i+1;
end
q=n;
for i=n:-1:1
for j=1:1:q
if E(j,i)==0;
E(j,:)=E(j,:);
else
E(j,:)=E(j,:)/E(j,i);
end
end
for k=1:1:i-1
E(k,:)=E(k,:)-E(i,:);
end
q=q-1;
end
X=E(:,n+1);
fprintf(****The Solution is****\n)X

댓글 수: 3

Razi Naji
Razi Naji 2021년 2월 8일
Thanks sir
Martin
Martin 2023년 5월 17일
The lack of tabs/formatting broke my brain :D
DGM
DGM 2023년 5월 18일
No documentation, no formatting, invalid characters, improper indexing. To add insult to injury, you harass the user by forcing them to blindly enter matrices using input() without any explanation of how the inputs should be oriented-- and then you throw it away and force them to do it again n times.
Why post something that's not even working code? It's not an answer, so you're not helping anyone else. It's not a question, so you're not helping yourself either. What's the point of this bizarre ritual?
I was going to fix the formatting, but it belongs like this.

댓글을 달려면 로그인하십시오.

Vishwa Lawliet
Vishwa Lawliet 2022년 9월 3일
편집: DGM 2023년 5월 18일

0 개 추천

disp('bX = c')
%4
n = input('Enter the size of matrix')
%[2 1 -1 2; 4 5 -3 6; -2 5 -2 6; 4 11 -4 8]
b = input('Enter the elements of the Matrix b ' )
%[5;9;4;2]
c = input('Enter the elements of the Matrix c ' )
dett = det(b);
if dett == 0
disp('This system unsolvable because det(b) = 0 ')
else
a=[b c];
for i = 0:n-2
for j = 0:n-2-i
a(i+j+2:i+j+2,i+1:n+1)=(a(i+j+2:i+j+2,i+1:n+1).*(a(i+1,i+1)/a(i+j+2,i+1)))-a(i+1:i+1,i+1:n+1);
disp(a)
end
end
X=c';
for i = 0:n-1
X(n-i)=(a(n-i,n+1)-sum(a(n-i:n-i,1:n).*X)+a(n-i,n-i)*X(n-i))/a(n-i,n-i);
end
X=X';
disp(X)
end

댓글 수: 1

Deepak
Deepak 2023년 6월 6일
dett = det(b);
This is not valid as b is a row matrix

댓글을 달려면 로그인하십시오.

태그

질문:

2017년 5월 14일

편집:

2024년 3월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by