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

27 visualizzazioni (ultimi 30 giorni)
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 Commenti

Accedi per commentare.

Risposta accettata

James Tursa
James Tursa il 25 Ott 2016
Modificato: James Tursa il 25 Ott 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 Commenti
Tai Lopez
Tai Lopez il 21 Ott 2018
Modificato: Walter Roberson il 22 Ott 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)) -
Steven Lord
Steven Lord il 21 Ott 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.

Accedi per commentare.

Più risposte (2)

Chaya N
Chaya N il 25 Ott 2016
Modificato: Chaya N il 25 Ott 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 il 25 Ott 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

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by