The program to find the determinant of matrix

Here is the MATLAB program to find the determinant of a nxn matrix by the cofactor method.  I had to develop a separate function for each size of the matrix.  I may be wrong about having to do that – is there a single function that can be written to find the determinant of any nxn matrix using the cofactor method?

The mfile can be downloaded here.   Try the program for a 10×10 matrix – it took about 6 seconds of CPU time on my PC.  A 12×12 matrix determinant would take about 13 minutes of CPU time.  I stopped at a 12×12 matrix.  You can either write a function or generate the function via a program for matrices of 13×13 order and higher.

Contents

Finding the determinant of a matrix using the cofactor method

and comparing the CPU time with MATLAB det function

clc
clear all
format long

% n=Size of matrix
n=6;
% Choosing a matrix of nxn size with random numbers
A=rand(n,n);

% Calculating cputime by cofactor method
tbegin=cputime;
detval=det6(A);
TimeCrammer=cputime-tbegin;

% Calculating cputime by MATLAB det function
tbegin=cputime;
MatlabDet=det(A);
TimeMatlab=vpa(cputime-tbegin,32);

% Printing the times
fprintf('Size of matrix is %gx%g \n',n,n)
fprintf('Determinant by cofactor method = %g \n', detval)
fprintf('Determinant by Matlab function = %g \n', MatlabDet)
fprintf('Approximate CPU time taken by cofactor method = %g seconds\n',TimeCrammer)
fprintf('Approximate CPU time taken by MATLAB function = %e seconds\n',TimeMatlab)

Individual functions for determinant of a nxn matrix

function detvalue=det2(A)
detvalue=A(1,1)*A(2,2)-A(1,2)*A(2,1);
end

function detvalue=det3(A)
n=3;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det2(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det4(A)
n=4;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det3(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det5(A)
n=5;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det4(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det6(A)
n=6;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det5(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det7(A)
n=7;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det6(A(2:n,[1:j-1 j+1:n]));
end
end
function detvalue=det8(A)
n=8;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det7(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det9(A)
n=9;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det8(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det10(A)
n=10;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det9(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det11(A)
n=11;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det10(A(2:n,[1:j-1 j+1:n]));
end
end

function detvalue=det12(A)
n=12;
detvalue=0;
for j=1:1:n
    detvalue=detvalue+(-1)^(j+1)*A(1,j)*det11(A(2:n,[1:j-1 j+1:n]));
end
end
Size of matrix is 6x6 
Determinant by cofactor method = -0.0431 
Determinant by Matlab function = -0.0431 
Approximate CPU time taken by cofactor method = 0.140625 seconds
Approximate CPU time taken by MATLAB function = 1.562500e-02 seconds

The above mfile can be downloaded here.


This post is brought to you by

 

Leave a Reply