SlideShare a Scribd company logo
1 of 2
% Gaussian/Gauss-Jordan elimination - General progran
% just to be safe
clear
% Choosing the method
a = input('input ''g'' for Gauss or anything else for Gauss-Jordan', 's');
% Please input your augmented matrix
% Use square brackets at either side of your matrix
% Use commas to seperate elements on the same row
% Use semi-colons to seperate the different rows
% For example, x+2y=3, 4x+5y=6 becomes [1,2,3;4,5,6]
A = input('Input your matrix ');
% checking it's possible
for n = 1:size(A,1)
if A(n,n) == 0
fprintf('Matrix cannot be solved by Gaussian elimination unless you swap rows
n')
else
A
% Choosing each pivot
for b = 1:size(A,1)-1
%For each row
for r = size(A,1):-1:2
if b<r
T = A(r,b)/A(b,b);
else
break
end
% For each column
for c = 1:size(A,2)
A(r,c) = A(r,c) - T*A(b,c);
end
% Printing the row transformation
if (T~=0)
fprintf('R%f = R%f - %f*R%f n', r, r, T, b)
fprintf('n')
A
end
% Use all the ends
end
end
end
end
% Setting up a matrix for x values
x = zeros(1,size(A,1));
% Finding the x values using algebra
if a == 'g'
for r = size(A,1):-1:1
x(r) = A(r,size(A,2));
for b = (size(A,2)-1):-1:2
if r<b
x(r) = x(r) - x(b)*A(r,b);
end
end
x(r) = x(r)/A(r,r);
end
% Printing results
for b = 1:size(A,1)
fprintf('x%i=%f n', b, x(b))
end
return
end
% Gauss-Jordan elimination
c = size(A,2);
for r = size(A,1):-1:1
T = A(r,r);
A(r,r) = A(r,r)/T;
A(r,c) = A(r,c)/T;
% Printing row transformation
if (T~=1)
fprintf('R%f = R%f/%f n',r, r, T )
A
end
for b = 1:size(A,1)-1
if b<r
T = A(b,r)/A(r,r);
A(b,r) = A(b,r) - T*A(r,r);
A(b,c) = A(b,c) - T*A(r,c);
end
% Printing row transformation
if (T~=0)
if (r>b)
fprintf('R%f = R%f - %fR%f n', r, r, T, b)
A
end
end
end
end

More Related Content

What's hot

Lesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative TheoryLesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative Theory
Sharon Henry
 
Lesson 3.3 3.4 - 1 st and 2nd Derivative Information
Lesson 3.3 3.4 - 1 st and 2nd Derivative InformationLesson 3.3 3.4 - 1 st and 2nd Derivative Information
Lesson 3.3 3.4 - 1 st and 2nd Derivative Information
Sharon Henry
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
Maria Theresa
 
Financialmodeling
FinancialmodelingFinancialmodeling
Financialmodeling
Talal Tahir
 
This is a ______ graph
This is a   ______ graphThis is a   ______ graph
This is a ______ graph
Chloe
 

What's hot (19)

Lesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative TheoryLesson 4.3 First and Second Derivative Theory
Lesson 4.3 First and Second Derivative Theory
 
Lesson 3.3 3.4 - 1 st and 2nd Derivative Information
Lesson 3.3 3.4 - 1 st and 2nd Derivative InformationLesson 3.3 3.4 - 1 st and 2nd Derivative Information
Lesson 3.3 3.4 - 1 st and 2nd Derivative Information
 
1.3.2C Equations of Lines
1.3.2C Equations of Lines1.3.2C Equations of Lines
1.3.2C Equations of Lines
 
Alg2 lesson 9-3
Alg2 lesson 9-3Alg2 lesson 9-3
Alg2 lesson 9-3
 
C programming
C programmingC programming
C programming
 
Main code for images
Main code for imagesMain code for images
Main code for images
 
GUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLEGUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLE
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
 
1.4.3 Slopes and Equations of Lines
1.4.3 Slopes and Equations of Lines1.4.3 Slopes and Equations of Lines
1.4.3 Slopes and Equations of Lines
 
Graphing rational functions
Graphing rational functionsGraphing rational functions
Graphing rational functions
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
1.4.4 Parallel and Perpendicular Line Equations
1.4.4 Parallel and Perpendicular Line Equations1.4.4 Parallel and Perpendicular Line Equations
1.4.4 Parallel and Perpendicular Line Equations
 
Practical Data Science : Data Cleaning and Summarising
Practical Data Science : Data Cleaning and SummarisingPractical Data Science : Data Cleaning and Summarising
Practical Data Science : Data Cleaning and Summarising
 
Financialmodeling
FinancialmodelingFinancialmodeling
Financialmodeling
 
This is a ______ graph
This is a   ______ graphThis is a   ______ graph
This is a ______ graph
 
Trapezoidal rule
Trapezoidal ruleTrapezoidal rule
Trapezoidal rule
 
Tutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational FunctionsTutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational Functions
 
Frito lay
Frito layFrito lay
Frito lay
 
19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)19 - Scala. Eliminators into dependent types (induction)
19 - Scala. Eliminators into dependent types (induction)
 

Viewers also liked

Métodos directos para la solución de sistemas de ecuaciones lineales
Métodos directos para la solución de sistemas de ecuaciones linealesMétodos directos para la solución de sistemas de ecuaciones lineales
Métodos directos para la solución de sistemas de ecuaciones lineales
Mileacre
 
linear equation system with 2 and 3 variables
linear equation system with 2 and 3 variableslinear equation system with 2 and 3 variables
linear equation system with 2 and 3 variables
Wanda Sari
 
Linear equation in two variables
Linear equation in two variablesLinear equation in two variables
Linear equation in two variables
MERBGOI
 
Lesson 9: Gaussian Elimination
Lesson 9: Gaussian EliminationLesson 9: Gaussian Elimination
Lesson 9: Gaussian Elimination
Matthew Leingang
 
Linear equation in two variable
Linear equation in two variableLinear equation in two variable
Linear equation in two variable
Ramjas College
 
Relations & functions.pps
Relations  &  functions.ppsRelations  &  functions.pps
Relations & functions.pps
indu psthakur
 
Math functions, relations, domain & range
Math functions, relations, domain & rangeMath functions, relations, domain & range
Math functions, relations, domain & range
Renee Scott
 

Viewers also liked (15)

Métodos directos para la solución de sistemas de ecuaciones lineales
Métodos directos para la solución de sistemas de ecuaciones linealesMétodos directos para la solución de sistemas de ecuaciones lineales
Métodos directos para la solución de sistemas de ecuaciones lineales
 
linear equation system with 2 and 3 variables
linear equation system with 2 and 3 variableslinear equation system with 2 and 3 variables
linear equation system with 2 and 3 variables
 
Gaussian elimination
Gaussian eliminationGaussian elimination
Gaussian elimination
 
Linear equation in two variables
Linear equation in two variablesLinear equation in two variables
Linear equation in two variables
 
Lesson 9: Gaussian Elimination
Lesson 9: Gaussian EliminationLesson 9: Gaussian Elimination
Lesson 9: Gaussian Elimination
 
Linear equation in 2 variables
Linear equation in 2 variablesLinear equation in 2 variables
Linear equation in 2 variables
 
linear equation and gaussian elimination
linear equation and gaussian eliminationlinear equation and gaussian elimination
linear equation and gaussian elimination
 
Gaussian Elimination Method
Gaussian Elimination MethodGaussian Elimination Method
Gaussian Elimination Method
 
Single and Double Elimination TOURNAMENT
Single  and Double Elimination TOURNAMENTSingle  and Double Elimination TOURNAMENT
Single and Double Elimination TOURNAMENT
 
Linear equation in two variable
Linear equation in two variableLinear equation in two variable
Linear equation in two variable
 
Generally accepted accounting principles
Generally accepted accounting principlesGenerally accepted accounting principles
Generally accepted accounting principles
 
Relations & functions.pps
Relations  &  functions.ppsRelations  &  functions.pps
Relations & functions.pps
 
Different types of functions
Different types of functionsDifferent types of functions
Different types of functions
 
Math functions, relations, domain & range
Math functions, relations, domain & rangeMath functions, relations, domain & range
Math functions, relations, domain & range
 
LINEAR EQUATION IN TWO VARIABLES PPT
LINEAR EQUATION  IN  TWO VARIABLES PPTLINEAR EQUATION  IN  TWO VARIABLES PPT
LINEAR EQUATION IN TWO VARIABLES PPT
 

Gaussian_GaussJordan

  • 1. % Gaussian/Gauss-Jordan elimination - General progran % just to be safe clear % Choosing the method a = input('input ''g'' for Gauss or anything else for Gauss-Jordan', 's'); % Please input your augmented matrix % Use square brackets at either side of your matrix % Use commas to seperate elements on the same row % Use semi-colons to seperate the different rows % For example, x+2y=3, 4x+5y=6 becomes [1,2,3;4,5,6] A = input('Input your matrix '); % checking it's possible for n = 1:size(A,1) if A(n,n) == 0 fprintf('Matrix cannot be solved by Gaussian elimination unless you swap rows n') else A % Choosing each pivot for b = 1:size(A,1)-1 %For each row for r = size(A,1):-1:2 if b<r T = A(r,b)/A(b,b); else break end % For each column for c = 1:size(A,2) A(r,c) = A(r,c) - T*A(b,c); end % Printing the row transformation if (T~=0) fprintf('R%f = R%f - %f*R%f n', r, r, T, b) fprintf('n') A end % Use all the ends end end end end % Setting up a matrix for x values x = zeros(1,size(A,1));
  • 2. % Finding the x values using algebra if a == 'g' for r = size(A,1):-1:1 x(r) = A(r,size(A,2)); for b = (size(A,2)-1):-1:2 if r<b x(r) = x(r) - x(b)*A(r,b); end end x(r) = x(r)/A(r,r); end % Printing results for b = 1:size(A,1) fprintf('x%i=%f n', b, x(b)) end return end % Gauss-Jordan elimination c = size(A,2); for r = size(A,1):-1:1 T = A(r,r); A(r,r) = A(r,r)/T; A(r,c) = A(r,c)/T; % Printing row transformation if (T~=1) fprintf('R%f = R%f/%f n',r, r, T ) A end for b = 1:size(A,1)-1 if b<r T = A(b,r)/A(r,r); A(b,r) = A(b,r) - T*A(r,r); A(b,c) = A(b,c) - T*A(r,c); end % Printing row transformation if (T~=0) if (r>b) fprintf('R%f = R%f - %fR%f n', r, r, T, b) A end end end end