Professional Documents
Culture Documents
Gauss Jordan - Algorithm and Matlab Program
Gauss Jordan - Algorithm and Matlab Program
MOHIT SINGH
ASTHA NAYAK
NIRBHAY KUMAR
AMBAR DIXIT
Algorithm
1. Normalize the first equation by dividing it by its pivot element.
2. Eliminate x1 term from all the other equations.
3. Now, normalize the second equation by dividing it by its pivot element
4. Eliminate x2 from all the equations above and below the normalized pivotal equation.
5. Repeat this process until xn is eliminated from all but the last equation
6. The resultant vector is the solution vector
Program Code
function [x,err]=gaussjordan(A,b)
% x is the solution vector
% err is the error value
% A is the input matrix of size n by m
% b is the input matrix of length n
[n,m]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(n,1); % calling function zero
if n ~= m
disp('error: n~=m'); % displaying error if found
err = 1; % setting err to 1
end
if length(b) ~= n % finding the length of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2; % setting error to 2
else
if size(b,2) ~= 1
b=b'; % Assigning the value of inverted matrix b to matrix b
end
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying error in matrix B
err = 3; % setting error to 3
end
end
if err == 0 % if no error is found
Aa=[A,b]; % Defining the augmented matrix
for i=1:n % iterating from 1 to n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1)); % Calling gauss_pivot
function to get the pivot element
if err == 0
Aa(1:n,i:n+1)=gaussjordan_step(Aa(1:n,i:n+1),i); % Calling
gaussjordan_step function to normalize the augmented matrix by its pivot
element
end
end
x=Aa(:,n+1); % Assigning the values obtained to the solution vector
end
A=0; % Setting the matrix A to 0
end
Output
[x,err]=gaussjordan([1 1 1;2 3 5; 4 0 5],[5 ; 8; 2])
x =
3
4
-2
err =