Determinant and inverse of a 3 X 3 matrix Issue

Hi I have a question:
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 should be named invanddet3by3. The function takes a generic 3 X 3 matrix as input, and returns two outputs: the determinant and the inverse. It should do the following few things:
It calculates 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.
However I MUST USE THE FUNCTION invanddet2by2sol provided to me which is :
function [ determinant, inverse ] = invanddet2by2sol( A )
% INVANDDET2BY2 Calculates the determinant and the inverse of a 2 X 2 matrix
determinant = A(1,1)*A(2,2)- A(1,2)* A(2,1); % calculates the determinant
if determinant ==0 % if the matrix is singular
inverse = []; % inverse does not exist
else
inverse = [A(2,2) -A(1,2); -A(2,1) A(1,1)]./determinant; % calculates the inverse
end
Here is my attempt :
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 = [(A(2,2) * A(3,3)) - (A(3,2) * A(2,3))]; % Cofactors 3x3 matrix A
A12 = - [(A(2,1) * A(3,3)) - (A(3,1) * A(2,3))];
A13 = [(A(2,1) * A(3,2)) - (A(3,1) * A(2,2))];
A21 = - [(A(1,2) * A(3,3)) - (A(3,2) * A(1,3))];
A22 = [(A(1,1) * A(3,3)) - (A(3,1) * A(1,3))];
A23 = - [(A(1,1) * A(3,2)) - (A(3,1) * A(1,2))];
A31 = [(A(1,2) * A(2,3)) - (A(2,2) * A(1,3))];
A32 = - [(A(1,1) * A(2,3)) - (A(2,1) * A(1,3))];
A33 = [(A(1,1) * A(2,2)) - (A(2,1) * A(1,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
end
My code passes all the tests for calculating the determinant and inverse correctly, but its saying: 'The submission must contain the following functions or keywords: invanddet2by2sol You MUST use the function invanddet2by2sol!'.
I have tried many things but have been unsuccessful.
Would appreciate the help,
Thanks very much.

1 Comment

Is one of the things that you have tried using the function provided to you that you MUST use?!

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 26 Oct 2016
Edited: Matt J on 26 Oct 2016
In the code that you have provided, your cofactor calculations in fact consist of many 2x2 determinant calculations
A11 = [(A(2,2) * A(3,3)) - (A(3,2) * A(2,3))]; % Cofactors 3x3 matrix A
A12 = - [(A(2,1) * A(3,3)) - (A(3,1) * A(2,3))];
A13 = [(A(2,1) * A(3,2)) - (A(3,1) * A(2,2))];
etc...
You are probably intended to use invandet2by2sol to do these 2x2 determinant calculations.

6 Comments

In my code, I have included the cofactor calculations because I want to calculate the determinant and inverse of a 3 x 3 matrix not 2 x 2. If i use invandet2by2sol, this is just for 2 x 2 calculations of determinant and inverse, but the question wants 3 x 3 determinant and inverse, so i am confused on how to implement the function invanddet2by2sol in my code.
Matt J
Matt J on 27 Oct 2016
Edited: Matt J on 27 Oct 2016
As I said, each co-factor that you are currently computing explicitly can instead be obtained using invandet2by2sol. The co-factors are themselves just determinants of 2x2 matrices (up to a sign), which is what invandet2by2sol is designed to calculate. That is the reason I believe that invandet2by2sol was given to you.
@Karan: E.g.,
A11 = [(A(2,2) * A(3,3)) - (A(3,2) * A(2,3))];
can be rewritten as:
A11 = invanddet2by2sol( "a 2x2 matrix made up of appropriate matrix A parts" );
You just need to fill in the code for the appropriate 2x2 matrix.
I have amended my code, but now i am failing a different test.
Here is my new 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
end
New error : Undefined function 'invanddet2bysol' for input arguments of type 'double'.
Error in invanddet3by3 (line 5) A11 = invanddet2bysol([A(2,2), A(2,3); A(3,2), A(3,3)]); % Cofactors 3x3 matrix A
Surely it is obvious what the problem is there?! You have to get into the habit of rereading your code to find basic errors and you need o get used to reading the error message and interpreting it. Usually the errors are exactly as they say.
Yeah sorry silly mistake. Thanks for the tip.

Sign in to comment.

More Answers (1)

You are getting this error because in your function
invanddet3by3(A)
there is NO CALL to function
invanddet2by2sol(...)

2 Comments

Yes thank you, i have acknowledged this now but i am having a problem calling the invandet2by2sol function. Is it
function [ determinant, inverse] = invanddet3y3(invandet2by2sol(A));
In the method of cofactors for comptuting det of a 3x3 matrix, you need to compute det of 3 cofactors 2x2. Here it is a "tutorial"..
Invoke invandet2by2sol(..) for computing the 2x2 determinants.
If this answer heped you, please accept it.

Sign in to comment.

Categories

Tags

No tags entered yet.

Asked:

on 26 Oct 2016

Commented:

on 27 Oct 2016

Community Treasure Hunt

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

Start Hunting!