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

40 visualizaciones (últimos 30 días)
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
  4 comentarios
Walter Roberson
Walter Roberson el 2 de Nov. de 2018
Is it saying that invanddet3by3 is undefined, or is it saying that invanddet2by2sol is undefined?

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 2 de Nov. de 2018
Your code invokes
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 comentarios
Tai Lopez
Tai Lopez el 2 de Nov. de 2018
Editada: Tai Lopez el 2 de Nov. de 2018
yes, sorry, my bad. I've been playing around with the code and accidentally deleted the "2". It has now been corrected but the function is still undefined.
Walter Roberson
Walter Roberson el 2 de Nov. de 2018
Editada: Walter Roberson el 2 de Nov. de 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.

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by