In: Computer Science
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the inverse function directly when writing your code. Here is the starter code that we are providing to help you get started:
%x is the input 2x2 matrix. function inverse_ret = findInverse(x)
. . .
%inverse_ret is the variable that will contain the inverse of the input x end
Inverse function:
%x is the input 2x2 matrix.
function inverse_ret = findInverse(x)
d = x(1,1) * x(2,2) - x(1,2) * x(2,1);
if( d == 0 )
disp( "The given matrix is singular (not invertible)" );
else
inverse_ret = [ x(2,2), -1 * x(1,2); -1 * x(2,1), x(1,1)] /
d;
end
%inverse_ret is the variable that will contain the inverse of the
input x
end
verifying code:
result :
1) if x=[4,7;2,6]
2)if x=[3,4;6,8]