Inverse.m
clc;
clear all;
close all;
disp('Inverse of a matrix using Adjoint Method')
disp('------------------------------------------------')
prompt = 'Enter the order of the matrix ';
m = input(prompt);
% initializes the variable M
M=zeros(m,m);
N=zeros(m,m);
% Entering random values in matrices
for i=1:m
for j=1:m
M(i,j)=random('unid',10);
end
end
d = detrminant(M,m);
disp('The determinant is ');
disp(num2str(d,2));
if d==0
disp('Matrix is not inversible');
else
cofactors(d,M,m,N,2,m); % N,2,m for separating division operation from inverse operation
end
detrminant.m
function d=detrminant(M,m)
det=0;
s=1;
N=zeros(m,m);
if (m==1)
d=M(1,1); return
else
det = 0;
for c=1:m
p = 1;
q = 1;
for i=1:m
for j=1:m
if (i~=1 && j~=c)
N(p,q)=M(i,j);
if (q<(m-1))
q=q+1;
else
q=1;
p=p+1;
end
end
end
end
det=det+s*(M(1,c)*detrminant(N,m-1));
s=-1*s;
end
end
d=det; return
cofactors.m
function cofactors(det,M,m,Q,u,f)
N=zeros(m,m);
fac=zeros(m,m);
for q=1:m
for p=1:m
m1 = 1;
n1 = 1;
for i=1:m
for j=1:m
if (i~=q && j~=p)
N(m1,n1) = M(i,j);
if (n1 < (m - 1))
n1=n1+1;
else
n1 = 1;
m1=m1+1;
end
end
end
end
fac(q,p)=power(-1,q+p)*detrminant(N,m-1);
end
end
trans(det,M,fac,m,Q,u,f); % transpose of cofactor matrix i.e. adjoint matrix
clc;
clear all;
close all;
disp('Inverse of a matrix using Adjoint Method')
disp('------------------------------------------------')
prompt = 'Enter the order of the matrix ';
m = input(prompt);
% initializes the variable M
M=zeros(m,m);
N=zeros(m,m);
% Entering random values in matrices
for i=1:m
for j=1:m
M(i,j)=random('unid',10);
end
end
d = detrminant(M,m);
disp('The determinant is ');
disp(num2str(d,2));
if d==0
disp('Matrix is not inversible');
else
cofactors(d,M,m,N,2,m); % N,2,m for separating division operation from inverse operation
end
detrminant.m
function d=detrminant(M,m)
det=0;
s=1;
N=zeros(m,m);
if (m==1)
d=M(1,1); return
else
det = 0;
for c=1:m
p = 1;
q = 1;
for i=1:m
for j=1:m
if (i~=1 && j~=c)
N(p,q)=M(i,j);
if (q<(m-1))
q=q+1;
else
q=1;
p=p+1;
end
end
end
end
det=det+s*(M(1,c)*detrminant(N,m-1));
s=-1*s;
end
end
d=det; return
cofactors.m
function cofactors(det,M,m,Q,u,f)
N=zeros(m,m);
fac=zeros(m,m);
for q=1:m
for p=1:m
m1 = 1;
n1 = 1;
for i=1:m
for j=1:m
if (i~=q && j~=p)
N(m1,n1) = M(i,j);
if (n1 < (m - 1))
n1=n1+1;
else
n1 = 1;
m1=m1+1;
end
end
end
end
fac(q,p)=power(-1,q+p)*detrminant(N,m-1);
end
end
trans(det,M,fac,m,Q,u,f); % transpose of cofactor matrix i.e. adjoint matrix
trans.m
function trans(det,mat,fac,r,Q,u,f)
N=zeros(r,r);
inv=zeros(r,r);
for i=1:r
for j=1:r
N(i,j)=fac(j,i);
end
end
for i=1:r
for j=1:r
inv(i,j)=N(i,j) / det;
end
end
disp('=============================================');
disp(' THE INVERSE OF THE MATRIX');
disp('=============================================');
disp(inv);
nonsingular(mat,inv,r,Q,u,f);
nonsingular.m
function nonsingular(mat,inv,r,Q,u,f)
if (u==2)
count=0;
I=zeros(r,r);
for i=1:r
for j=1:r
sum=0;
for k=1:r
sum=sum+mat(i,k)*inv(k,j);
end
I(i,j)=sum;
end
end
disp('=============================================');
disp(' CHECK FOR SINGULAR OR NON-SINGULARITY');
disp('=============================================');
disp(I);
for i=1:r
for j=1:r
if(i==j && int8(I(i,j))==1)
count=count+1;
end
end
end
if(count==r)
disp('Non-Singular/Invertible Inverse Matrix');
else
disp('Singular/Non-Invertible Inverse Matrix');
end
elseif (u==1)
DIV=zeros(f,r);
for i=1:f
for j=1:r
sum=0;
for k=1:r
sum=sum+Q(i,k)*inv(k,j);
end
DIV(i,j)=sum;
end
end
disp('=============================================');
disp(' DIVISION OF TWO MATRICES');
disp('=============================================');
disp(DIV);
end
No comments:
Post a Comment