How can i create a function that does this?: function D= inverses (A) which take
ID: 3792323 • Letter: H
Question
How can i create a function that does this?:
function D=inverses(A)
which takes as an input an matrix A.
First, the function has to determine whether A is invertible (you may want to use the function rank to do that). If A is not invertible, the function has to return an empty matrix
D=[ ];
and terminates with a message “Matrix A is not invertible”.
If A is invertible, then the function reduces the matrix [A eye(n)] into the reduced echelon form and returns the matrix D that is the inverse of the matrix A. You can use a MATLAB built-in function rref for this part.
Explanation / Answer
Matrix inverse can be done using Gauss-Jordan elimination method.
We have two files here.
1) main.m (Main file that will have the Matrix (A) and calls inverse function)
2) inverse.m (This generates inverse of Matrix (A) using Gauss-Jordan elimination method)
---------------------------------------------------------------------------
1) main.m
% Sample matrix, 3x3 matrix
A=[3,0,2;2,0,-2;0,1,1];
% Find inverse of matrix A
D = inverse(A);
disp('Inverse of Matrix A:');
% Print Inverse of Matrix A
if(isempty(D))
% If D is empty, print not invertible
disp('A is not invertible.');
else
% Print D
disp(D);
end
---------------------------------------------------------------------------
2) inverse.m
function D = inverse(A)
% r: number of rows in Matrix A
% c: number of colums in Matrix A
[r, c] = size(A);
% Calculate the rank (rk) of matrix A
rk = rank(A);
% Inverse of a non-square matrix is not possible, r==c
% compare dimensions with rank
if (rk == r) && (rk == c)
% rref to reduced row echelon form
rre = rref([A eye(r)]);
D = rre(:, (c+1):(r+c));
else
% If A is not inverstible then set inverse to empty matrix
D = [];
end;
---------------------------------------------------------------------------
OUTPUT:
Inverse of Matrix A:
0.2000 0.2000 0
-0.2000 0.3000 1.0000
0.2000 -0.3000 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.