## 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.