%% A program which can calculate inverse of a given matrix by
%% by Gauss Elimination Method using complete pivot.
M = [0.2 1 0.2 0.0588 0.02702;0.1 0.5 0.5 0.1 0.0385;0.0588 0.2 1 0.2 0.0588 ;0.0385 0.1 0.5 0.5 0.1; 0.027 0.0589 0.2 1 0.2]; % Given matrix whose inverse i s to be found A = M; Ni = length(A(1,:)); % number of rows of given matrix Nj =length(A(:,1)); if abs(det(A))<0.00000000001
fprintf('The matrix determinant is zero, the result found may not be cor rect');
end px =1:Ni; qy =1:Ni;
%%main loop; find pivot entry in columns qy[k], qy[k+1], ...,qy[n-1] %% and in rows px[k], px[k+1],.....px[n-1]
for k = 1: Ni-1
pct = k; qdt = k;
aet =0;
for i=k:Ni
for j=k:Ni
tmp = abs(A(uint8(px(i)),uint8 (qy(j))));
if (tmp>aet)
aet = tmp; pct = i;qdt = j;
end
end
end
if ~aet fprintf('pivot is zero, No inverse possible \n'); end
% Swap the rows and columns to get highest number in the diagonal
px([k pct]) = px([pct k]);%//swap px[k] and px[pct]
qy([k qdt]) = qy([qdt k]) ; %% eliminate column entries (make them zero) logically below and right t o the entry mx[px[k]][qy[k]]
for i=k+1:Ni
if A(px(i),qy(k)) ~= 0 % if the entry is not equal to zero
mult = A(px(i),qy(k))/A(px(k),qy(k)); % get the multiplying fact or
A(px(i),qy(k))= mult;
for j=k+1:Ni
A(px(i),qy(j)) = A(px(i),qy(j)) - mult*A(px(k),qy(j)); % ent ry will become zero