Determinant and Inverse problem

I need help with the following; a function takes a generic 2×2 matrix as input, and returns two outputs: the determinant and the inverse. Also, if the determinant is zero, the inverse is set to be an empty matrix (value []), or if the determinant is non-zero, then it calculates the inverse. This needs to be done without using det() and inv() functions. Thank you for your time.

7 Comments

What part do you need help with then? I assume you have read up on the definitions of determinant and inverse of a 2*2 matrix?
Ben
Ben on 30 Oct 2015
Edited: Ben on 30 Oct 2015
I know what they both are, just unsure how to code what the det() and inv() functions would do manually. I am a total beginner, I've coded a simple .m file but not much else. Thanks.
The determinant of a 2x2 Matrix is a*d-c*b. For the inverse: http://www.mathwords.com/i/inverse_of_a_matrix.htm
Try to implement the functions and if you have errors or problems upload your .m file.
Best regards,
Johannes
This appears to be homework. You won't learn anything by being given the solution. You will learn by trying to write it yourself.
So far I have
A = [2 3; 8 7]
Determinant =(A(1,1)*A(2,2))-(A(1,2)*A(2,1))
and this runs fine in matlab! I'm having real problems getting my head around the manual inverse, my teacher uploaded a video explaining WHAT it is but not HOW to even remotely start coding it.
Ben - a link for the algorithm in finding the inverse of a 2x2 matrix was posted in @Johannes' comment. Look at the Shortcut for 2x2 matrices and you should be able to figure out what is missing. (You have the determinant, so half the work is complete.)
Geoff - I have attached my .m file. I don't understand whether I am required to maniuplate individual matrix values using code, but my code works anyway (I hope!). Thanks everyone for your time.

Sign in to comment.

Answers (1)

Jan
Jan on 1 Nov 2015
Edited: Jan on 1 Nov 2015
You want to determine the inverse of a 2x2 matrix. So write down the definition paper:
[a, b; c, d] * [ai, bi; ci, di] = [1, 0; 0, 1]
This can be written as 4 equations with 4 unknowns and you can solve this manually. You get e.g.:
inv_A(2,2) = -A(1,2) / (A(1,1) * A(2,2) - A(1,2) * A(2,1))
Perhaps you recognize some parts of this expression?

6 Comments

This is what I have so far!
Ben - so using what Jan has provided, how would you modify your code for any 2x2 matrix?
Ben
Ben on 1 Nov 2015
Edited: Ben on 1 Nov 2015
Geoff - I think I sussed it! Many hours later though. Would you mind checking over my code? I did a 3x3 also, very chuffed. I'd also be grateful if you have a tip on how to get rid of the ''ans='' bit at the end -- if I understand correctly it's doing this because of the nature of the variables in the function I defined?
Ben - both of your functions have hard-coded matrices and so do not satisfy the requirement to allow generic 2x2 (and presumably 3x3) matrices as input so you must correct this.
Also, in order to suppress output (from within the function) put semi-colons at the end of your line. For the ans that you see when you invoke your function, make sure that you assign the output from your function to two variables and, again, use a semi-colon
[det, inv] = invanddet2by2(A);
where A is a 2x2 matrix.
Ben
Ben on 1 Nov 2015
Edited: Ben on 1 Nov 2015
Is the 3x3 better? My 2x2 is broken now because I still can't get my head around the diagonal code that I'd have to write for a random 2x2 matrix (when you flip 1,1 and 2,2 and make 1,2 and 2,1 negative)
Ben - I don't understand the diagonal code in your 2x2 matrix inverse function which is still hard-coded as
DiagonalA2by2 = [7 -3; -8 2];
Again, look at the link posted by @Johannes in his comment. It will tell you exactly how to invert a 2x2 matrix that has the form of
A = [a b
c d]
where a, b, c, and d are real numbers. Start with that before proceeding to the 3x3 case (which your code still overwrites the input matrix with A3by3 = [1 2 3; 0 4 5; 1 0 6]).

Sign in to comment.

Categories

Asked:

Ben
on 30 Oct 2015

Commented:

on 1 Nov 2015

Community Treasure Hunt

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

Start Hunting!