Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

GROUP EFFORT BY :-

MOHIT SINGH

ASTHA NAYAK

NIRBHAY KUMAR

AMBAR DIXIT

UTKARSH PRATAP SINGH

Gauss Jordan Method

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

function A1=gaussjordan_step(A,i) % calling of function gaussjordan_step

[n, m]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1); % assigning the value of first element of i th row of A1 to s1
A1(i,:) = A(i,:)/s; % Dividing A by s
k=[[1:i-1],[i+1:n]]; % Calculating k for the iterations
for j=k
s=A1(j,1); % obtaining the multiplier s
A1(j,:)=A1(j,:)-A1(i,:)*s; % to make the elements below diagonals as zero or
simply, eliminating those elements

end

function [A1,err]=gauss_pivot(A) % calling of function gauss_pivot


[n, m]=size(A); % finding the size of matrix A
A1=A; % process of assigning
err = 0; % error flag
if A1(1,1) == 0
check = True; % logical(1) - TRUE
i = 1; % setting i = 1
while check
i = i + 1; % increment statement for while loop
if i > n
disp('error: matrix is singular'); % displaying the error
err = 1; % setting the error equal to 1
check = False; % setting check to False
else
if A(i,1) ~= 0 && check
check = False; % setting check to False
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:); % Assigning values of A1(1,:) to A1(i,:)
A1(1,:)=b; % Here, b is used to swap the values of A1(1,:) to A1(i,:)
end
end
end
end
% This marks the end of the function

Output
[x,err]=gaussjordan([1 1 1;2 3 5; 4 0 5],[5 ; 8; 2])

x =

3
4
-2

err =

You might also like