In: Electrical Engineering
how to write coding in matlab using gauss elimination method for equation
10a + 50b + 20c + 10d = 100
5a + 15b + 75c - 25d=200
25a -15c - 5d = 300
10a + 20b - 30c + 100d = 400
MATLAB code is given below in bold letters.
function C = gauss_elimination(A,B) % defining the
function
A= [ 10 50 20 10; 5 15 75 -25;25 0 -15 -5; 10 20 -30 100 ] %
Inputting the value of coefficient matrix
B = [100; 200; 300; 400] % % Inputting the value of coefficient
matrix
i = 1; % loop variable
X = [ A B ];
[ nX mX ] = size( X); % determining the size of matrix
while i <= nX % start of loop
if X(i,i) == 0 % checking if the diagonal elements are zero or
not
disp('Diagonal element zero') % displaying the result if there
exists zero
return
end
X = elimination(X,i,i); % proceeding forward if diagonal elements
are non-zero
i = i +1;
end
C = X(:,mX);
function X = elimination(X,i,j)
% Pivoting (i,j) element of matrix X and eliminating other
column
% elements to zero
[ nX mX ] = size( X);
a = X(i,j);
X(i,:) = X(i,:)/a;
for k = 1:nX % loop to find triangular form
if k == i
continue
end
X(k,:) = X(k,:) - X(i,:)*X(k,j); % final result
end
RESULT:
a b c and d are given below.
a = 15.1209
b = -3.3912
c = 3.7692
d = 4.2969