Monday, August 24, 2015

Inverse / Division of Matrix using Adjoint Method

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

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


Wednesday, September 17, 2014

Trapezoidal Method of numerical integration in C

#include<stdio.h>
#include<conio.h>

float f(float x){
float r;
r=2*x+1;
return r;
}

void main(){

float output;
float a,b;
float h,s;
int i,j;
int n;
clrscr();
printf("===========================================\n");
printf("           TRAPEZOIDAL RULE\n");
printf("===========================================\n");
printf("Enter the integral limit:-\n");
printf("-------------------------------------------\n");
printf("Lower limit value a: \n");
scanf("%f",&a);
printf("Upper limit value b: \n");
scanf("%f",&b);
printf("-------------------------------------------\n");
printf("Enter number of total intervals: \n");
scanf("%d",&n);
h=0.0;
h=(b-a)/n;
printf("____________________________________________\n");
printf("Resultant value of h is %.2f \n",h);
s=0.0;
s=f(a)+f(b);
printf("Resultant value of f(a) + f(b) is %.2f \n",s);
for (i=1;i<=n-1;i++)
{
s=s+2*f(a+i*h);
}
printf("Resultant value of whole summation of f(-) is %.2f\n",s);
output=1;
output=(h/2)*s;
printf("Output value of the integral is %.2f \n",output);
printf("____________________________________________\n");
printf("             GOOD LUCK\n");
getch();
}

Simpson's One-Third Rule of numerical integration in C

#include<stdio.h>
#include<conio.h>

float f(float x){
float r;
r=2*x+1;
return r;
}

void main(){

float output;
float a,b;
float h,s;
int i,j;
int n;
clrscr();
printf("===========================================\n");
printf("           SIMPSON'S ONE-THIRD RULE\n");
printf("===========================================\n");
printf("Enter the integral limit:-\n");
printf("-------------------------------------------\n");
printf("Lower limit value a: \n");
scanf("%f",&a);
printf("Upper limit value b: \n");
scanf("%f",&b);
printf("-------------------------------------------\n");
printf("Enter number of total intervals: \n");
scanf("%d",&n);
h=0.0;
h=(b-a)/(2*n);
printf("____________________________________________\n");
printf("Resultant value of h is %.2f \n",h);
s=0.0;
s=f(a)+f(b)+4*f(a+h);
printf("Resultant value of f(a) + f(b) is %.2f \n",s);
for (i=3;i<2*n;i+=2)
{
s+=2*f(a+(i-1)*h)+4*f(a+i*h);
}
printf("Resultant value of whole summation of f(-) is %.2f\n",s);
output=1;
output=(h/3)*s;
printf("Output value of the integral is %.2f \n",output);
printf("____________________________________________\n");
printf("             GOOD LUCK\n");
getch();
}