In: Mechanical Engineering
use matlab to create a function that finds inverse of 2x2 matrix and returns result. Should be a error check to make sure matrix is 2x2, and nonsingular.
Matlab code:
______________________________________________________________________________
a = [4 7; 2 6];
det_a = (a(1,1)*a(2,2) - a(1,2)*a(2,1));
s = size(a);
if det_a == 0 || s(1,1) ~= 2 || s(1,2) ~= 2
fprintf('Determinant of the provided matrix is zero or either size
of the matrix is not 2x2 hence inverse not possible')
else
a1 = [a(2,2) -a(1,2) ; -a(2,1) a(1,1)];
inv_a = (1/det_a)*a1
end
____________________________________________________________________________
Above code will use the direct method of finding inverse of an 2x2 matrix. If the matrix size is different than 2x2 or it is singular code will give an message that provided matrix is not correct.
Please provide the feedback.