# Gaussian Elimination technique by matlab

1.363 views (last 30 days)
Razi Naji on 14 May 2017
Answered: Omar rabaa on 31 Jul 2022 at 12:21
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
Muntasir on 15 Jun 2022
Jealryn obordo
'n' is the number of variables or equations. Here n=3

suraj kumar on 23 Feb 2020
% 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'
Muhammed Ali on 31 Dec 2020
hello sir what does variable m does in this code ?
like what is its purpose explained in your words

M Waqar Nadeem on 11 Mar 2021
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
##### 2 CommentsShowHide 1 older comment
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]'

pss CHARAN on 2 Dec 2020
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
Razi Naji on 8 Feb 2021
Thanks sir

vaman doski on 14 Apr 2021
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

Chatlthon John Pequiro on 4 May 2021
2x1 + x2 - x3 + 2x4 = 5 4x1 + 5x2 - 3x3 + 6x4 = 9 -2x1 + 5x2 - 2x3 + 6x4 = 4 4x1 + 11x2 - 4x3 + 8x4 = 2

Onur Yaprak on 9 Dec 2021
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)
x
end
end

nissankararao yaswanth on 18 Jan 2022
% 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

AISYAH NAD on 13 May 2022
1 1 4 = 0
-1 2 -3 = 0
2 -1 -3 = 0
GAUSS ELIMINATION METHOD

Omar rabaa on 31 Jul 2022 at 12:20
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 ]

Omar rabaa on 31 Jul 2022 at 12:20
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 ]

Omar rabaa on 31 Jul 2022 at 12:20
% 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

Omar rabaa on 31 Jul 2022 at 12:21
% 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

Omar rabaa on 31 Jul 2022 at 12:21