# Creating a function that calculates the determinant and inverse of a 3x3 matrix

38 views (last 30 days)
Tai Lopez on 30 Oct 2018
Edited: Walter Roberson on 2 Nov 2018
I need to create a function that calculates the determinant and the inverse of a generic 3 X 3 matrix with the method of the cofactors and the adjoint matrix. The function has to calculate the determinant using the cofactors. If the determinant is zero, the inverse is set to be an empty matrix. If the determinant is non-zero, then it calculates the inverse according to the algorithm. I also have to use the function "invanddet2by2sol".
I don't understand how to use that invanddet2by2sol function, i.e. where to put it. so far I have this code
function [ determinant, inverse ] = invanddet3by3( A )
%INVANDDET3BY3 Calculates the determinant and the inverse of a 3 X 3 matrix by using
% cofactors and adjoint matrix
A11 = invanddet2bysol([A(2,2), A(2,3); A(3,2), A(3,3)]); % Cofactors 3x3 matrix A
A12 = -(invanddet2by2sol([A(2,1), A(2,3); A(3,1), A(3,3)]));
A13 = invanddet2by2sol([A(2,1), A(2,2); A(3,1), A(3,2)]);
A21 = -(invanddet2by2sol([A(1,2), A(1,3); A(3,2), A(3,3)]));
A22 = invanddet2by2sol([A(1,1), A(1,3); A(3,1), A(3,3)]);
A23 = -(invanddet2by2sol([A(1,1), A(1,2); A(3,1), A(3,2)]));
A31 = invanddet2by2sol([A(1,2), A(1,3); A(2,2), A(2,3)]);
A32 = -(invanddet2by2sol([A(1,1), A(1,3); A(2,1), A(2,3)]));
A33 = invanddet2by2sol([A(1,1), A(1,2); A(2,1), A(2,2)]);
J = [ A11 A12 A13; A21 A22 A23; A31 A32 A33]; % Adjugate Matrix
determinant = ((A(1,1) * A11) + (A(1,2) * A12) + (A(1,3) * A13)); % Determinant of A
inverse = (1/determinant) * (J'); % Inverse of A
if determinant==0
inverse=[];
end Tai Lopez on 2 Nov 2018
invanddet3by3 is undefined

Walter Roberson on 2 Nov 2018
A11 = invanddet2bysol([A(2,2), A(2,3); A(3,2), A(3,3)]); % Cofactors 3x3 matrix A
which uses invanddet2bysol instead of invanddet2by2sol
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 2 Nov 2018
Edited: Walter Roberson on 2 Nov 2018
Please show the output of
which -all invanddet3by3
pwd
ls
I notice in your image that your file name is Untitled and that it is not saved to disk. You would need to Save that editor window; it will ask you what filename to save to and it would suggest invanddet3by3.m as the name, which is what you should use.