Creating a matrix that calculates inverse and determinants without using the det and inv commands
9 views (last 30 days)
Show older comments
Rafaela Stylianou
on 24 Oct 2016
Edited: Walter Roberson
on 22 Oct 2018
Hi, i have the following question:
Create a function that calculates the determinant and the inverse of a generic 2 X 2 matrix
The function should be named invanddet2by2. The function takes a generic 2 X 2 matrix as input, and returns two outputs: the determinant and the inverse. It should do the following few things:
It calculates the determinant
If the determinant is zero, the inverse is set to be an empty matrix (i.e. you assign the value [], that's squared brackets with no values inside, which for Matlab means an empty matrix)
If the determinant is non-zero, then it calculates the inverse
So far i have
M=[ 4 5;6 9]
d_correct=(M(1,1)*M(2,2))-(M(1,2)*M(2,1))
if d_correct==0
B=[0 0; 0 0]
else
Diagonal=[9 -5;-6 4]
Minv_correct=d_correct *(1./M)
'end'
and my codes fail the test for the inverse of the singular matrix and for the determinant of the singular matrix
Does anyone have an idea of what's gooing wrong?
3 Comments
Accepted Answer
James Tursa
on 25 Oct 2016
Edited: James Tursa
on 25 Oct 2016
Create a file in your working directory called invanddet2by2.m, e.g.
edit invanddet2by2.m
In that file, put the following function code (some function wrapper code with your code inside):
% Insert comments here describing function purpose, inputs, and outputs
function [B,d_correct] = invanddet2by2(M)
d_correct = (M(1,1)*M(2,2))-(M(1,2)*M(2,1));
if d_correct==0
B = [0 0; 0 0]; <-- Fix this line to return the empty matrix [] instead of a 0's matrix
else
Diagonal = [9 -5;-6 4]; <-- Fix this line to be a generic formula of M elements, not hard-coded values
Minv_correct = d_correct *(1./M); <-- Fix this line to use Diagonal, do the division in the correct order, and assign to B
end
Make changes and corrections to the lines that I have indicated.
3 Comments
Tai Lopez
on 21 Oct 2018
Edited: Walter Roberson
on 22 Oct 2018
I'm having a similar problem. so I've created a function, it works when I type any 2x2 matrix but it doesn't run. do you know what is wrong with it? I've also attached a picture which shows the error. This is what I have:
function [Inverse, Determinant] = invanddet2by2(M)
% INVANDDET2BY2 Calculates the determinant and the inverse of a 2 X 2 matrix.
% It calculates the determinant.
% 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
Determinant = (M(1,1)*M(2,2))-(M(1,2)*M(2,1))
if Determinant==0
Inverse = [];
else
Madj_correct = trace(M)*eye(size(M)) -
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193349/image.png)
Steven Lord
on 21 Oct 2018
You call your function with 0 input arguments. MATLAB doesn't know what it should use as the M matrix in your function. Call it and pass your matrix in as the input.
More Answers (2)
Chaya N
on 25 Oct 2016
Edited: Chaya N
on 25 Oct 2016
A singular matrix, by definition, is one whose determinant is zero. hence, it is non-invertible. In code, this would be represented by an empty matrix. Therefore (using the same variable name as in your code),
B = [];
For a non-singular matrix M, recall that M * inverse(M) = I, the identity matrix. This is the simplest expression you could use to generate your inverse matrix. Therefore,
Minv_correct = eye(size(M))/M; % Note that this is regular division, NOT element-wise operation
There is also another commonly used method, that involves the adjoint of a matrix and the determinant to compute the inverse as inverse(M) = adjoint(M)/determinant(M). This involves the additional step of computing the adjoint matrix. For a 2 x 2 matrix, this would be computed as adjoint(M) = trace(M)*I - M. Therefore,
Madj_correct = trace(M)*eye(size(M)) - M;
Minv_correct = Madj_correct/d_correct;
You could use either of the above methods to compute your inverse matrix.
0 Comments
Walter Roberson
on 25 Oct 2016
In the case of the determinant being 0, you are not setting the inverse to the empty matrix.
You are not using the variable Diagonal .
Your formula for obtaining the inverse from the determinant is incorrect. See https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!