Write a userdefined MATLAB function that calculates the inve

Write a user-defined MATLAB function that calculates the inverse of a square (n x n) matrix, using the Cramer function. For function name and arguments, use D = Inverse(A) . The input argument A is the matrix whose inverse is calculated. The function Inverse should first check if the matrix is square. If it is not, the message \"The matrix must be square!\" should appear on the screen. Use Inverse to calculate the inverse of the following matrix:

    3   5   9 10

   -4 0 -6   -8

    4 -8 7    -1

    4 -5   1   -7

Solution

The Matlab function Inverse.m

function D = Inverse(A) % The function to find the inverse of square matrix
N = size(A); % Geting the dimension of the matrix
if N(1)~=N(2) % if A is not a square matrix exit the function
    fprintf(\'The matrix must be square!\ \'); % with an error message
    return;% function retune
end
D = zeros(N); % initilizing the result matrix
R = 1:N;% row variable
C = 1:N;% column variabele
for i = 1:N % loop for column
     for j = 1:N % loop for row
         D(j,i) = (-1)^(i+j)*det(A(R(R~=i),C(C~=j))); % determin Aij s
     end
end
D = D./det(A); % devideing the matrix by determinent of A
end


Checking the function Inverse.m

>> A = [3 5 9 10; -4 0 -6 -8; 4 -8 7 -1;4 -5 1 -7];
>> D=Inverse(A)

D =

   -0.0492   -0.2344   -0.1694    0.2219
    0.1311    0.0918   -0.1038    0.0973
    0.1639    0.2148    0.1202   -0.0284
   -0.0984   -0.1689   -0.0055   -0.0896

>> H =inv(A)

H =

   -0.0492   -0.2344   -0.1694    0.2219
    0.1311    0.0918   -0.1038    0.0973
    0.1639    0.2148    0.1202   -0.0284
   -0.0984   -0.1689   -0.0055   -0.0896

Write a user-defined MATLAB function that calculates the inverse of a square (n x n) matrix, using the Cramer function. For function name and arguments, use D =

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site