MATLAB Answers

Creating a matrix that calculates inverse and determinants without using the det and inv commands

147 views (last 30 days)
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]
if d_correct==0
B=[0 0; 0 0]
Diagonal=[9 -5;-6 4]
Minv_correct=d_correct *(1./M)
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?

Sign in to comment.

Accepted Answer

James Tursa
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
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
Make changes and corrections to the lines that I have indicated.
Steven Lord
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.

Sign in to comment.

More Answers (2)

Chaya N
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.

Walter Roberson
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

Community Treasure Hunt

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

Start Hunting!

Translated by