In: Electrical Engineering
Show and explain Gauss-Jordan elimination using Matlab.
Ques:
Ans: To solve Gauss-Jordan elimination Method, i consider a linear equation of 4 variable.
the Below code to solve this by this method
Matlab code:
close all
clear all
clc
syms x y z w
%Converting Linear Simultaneous Equations to Matrix form
eqns=[6*x+2*y+2*z+4*w==1,-x+2*y+2*z-3*w==-1,
x+2*z+3*w==1,y+z+4*w==2];
vars=[ x y z w];
% a is the matrix of co-efficient
% b is the matrix of constants
[a,b]=equationsToMatrix(eqns,vars)
% m is the row
% c is the column
[m,c]=size(a);
j=1; %j th column
k=1;%k th row
n=1;% n th pivoting row
z=2;% Row operation always starts from 2
% Forward Elimination
% Shifting Loop for column operation
for i=1:c-1
%Making each desired column component zero
for r=z:m
% Checking if any Lower triangle is Already Zero or not
if a(r,j)==0
% If any is zero left the entire row as it is and skip the
step
a(r,:)=a(r,:);b(r,:)=b(r,:);
else
b(r,:)=((a(r,j)/a(k,j))*b(n,:))-b(r,:)
a(r,:)=((a(r,j)/a(k,j))*a(n,:))-a(r,:)
end
end
k=k+1;% Changing row after completion of inner loop
n=n+1;% Changing the pivoting row
z=z+1;% Setting a new condition for inner loop
j=j+1;% Changing column after completion of inner loop
end
% Converting all the diagonal elements to one
for row=1:m
b(row,:)=b(row,:)/a(row,row)
a(row,:)=a(row,:)/a(row,row)
end
D=c;
for q=1:D-1
for rowJ=m-1:-1:1
if a(rowJ,c)==0
a(rowJ,:)=a(rowJ,:);b(rowJ,:)=b(rowJ,:);
else
b(rowJ,:)=b(rowJ,:)-(a(rowJ,c)*b(m,:))
a(rowJ,:)=a(rowJ,:)-(a(rowJ,c)*a(m,:))
end
end
m=m-1;
c=c-1;
end
% x,y,z,w is nothing but cofficient of b
x = b(1)
y = b(2)
z = b(3)
w = b(4)
Result:
number of iteration is more, so i can't display whole result that's why i will show you final result. in this code you can see every iteration result.