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

조회 수: 8 (최근 30일)
Tai Lopez
Tai Lopez 2018년 10월 30일
편집: Walter Roberson 2018년 11월 2일
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
Walter Roberson
Walter Roberson 2018년 11월 2일
Is it saying that invanddet3by3 is undefined, or is it saying that invanddet2by2sol is undefined?

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 11월 2일
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
Tai Lopez
Tai Lopez 2018년 11월 2일
편집: Tai Lopez 2018년 11월 2일
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 2018년 11월 2일
편집: Walter Roberson 2018년 11월 2일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by