Вы находитесь на странице: 1из 4

New Era University

College of Engineering and Technology


Department of Electronics Engineering

LABORATORY REPORT NO. 2


GAUSS- JORDAN METHOD

NAME: LAPEÑA, LEANDRO I. SCHEDULE: TUESDAY 4-7PM

SECTION: 4BSECE-B INSTRUCTOR: ENGR. ESMERALDA C. MANIQUIS

RATING
ALGORITHM FLOWCHART

1. Start
2. Read the order of the matrix ‘n’
and read the coefficients of the
linear equations.
3. Do for k=1 to n
Do for l=k+1 to n+1
a[k][l] = a[k][l] / a[k][k]
End for l
Set a[k][k] = 1
Do for i=1 to n
if (i not equal to k) then,
Do for j=k+1 to n+1
a[i][j] = a[i][j] – (a[k][j] * a[i][k])
End for j
End for i
End for k
4. Do for m=1 to n
x[m] = a[m][n+1]
Display x[m]
End for m
5. Stop
ENGLISH MATLAB PROGRAM
function [x,err]= gauss_jordan_elim(A,b)
1. Write the augmented matrix of the A = [1 1 1;2 3 5; 4 0 5] % input for augmented matrix A
b = [5 ; 8; 2] % intput for matrix B
system. [n,m]=size(A); % finding the size of matrix A
2. Use row operations to transform the err =0; % calculation of error
augmented matrix in the form described x=zeros(n,1); % calling fuction zero
if n ~= m
below, which is called the reduced row disp('error: n~=m'); % displaying error if found
echelon form (RREF). (a) The rows (if err = 1;
any) consisting entirely of zeros are end % end of the scope of if
if length(b) ~= n % finding the legth of matrix B
grouped together at the bottom of the disp('error: wrong size of b'); % displaying error, if found
matrix. (b) In each row that does not err = 2;
consist entirely of zeros, the leftmost else
if size(b,2) ~= 1
nonzero element is a 1 (called a leading b=b';
1 or a pivot). (c) Each column that end % end of the scope of if-else
contains a leading 1 has zeros in all other if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying erron in
entries. (d) The leading 1 in any row is to matrix B
the left of any leading 1’s in the rows err = 3;
below it. end
end
3. Stop process in step 2 if you obtain a if err == 0
row whose elements are all zeros except Aa=[A,b];
the last one on the right. In that case, the for i=1:n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1));
system is inconsistent and has no if err == 0
solutions. Otherwise, finish step 2 and Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i);
read the solutions of the system from the end
end
final matrix. x=Aa(:,n+1);
end
A=0;
function A1=gauss_jordan_step(A,i) % calling of fuction
function

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


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:n]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
function [A1,err]=gauss_pivot(A) % calling of fucntion
[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 = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > n
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end

Вам также может понравиться