Create the following functions in MATLAB: function D=inverses(A) which takes as
ID: 3797857 • Letter: C
Question
Create the following functions in MATLAB: function D=inverses(A) which takes as an input annex 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. **Type the functions inverses in your diary file. **Run the function D=inverses(A) on the given choices of the matrices A in (a)-(d). A = [4 -6 7 -1 0 1 -5 2 -7 11 10 3 -7 9 19 -1] A = [1 -3 2 -4 3 9 -6 12 2 -1 4 2 -4 5 -3 7], A = magic (5) **Run a built-in MATLAB function inv on the matrices A for parts (c) and (d). % Write a comment in you diary file why the result obtained by using inv function is inaccurate for the matrix in part (d).Explanation / Answer
The MATLAB function for the finding inverse is given below:
%%function definition starts here:
%% Inverse finding
function D=inverses(A)
detA=det(A);
if detA==0
disp(' A is not Invertible');
D=[];
else
n=size(A);
B=[A eye(n)];
D_A=rref(B);
D=D_A(:,n+1:2*n);
end
%% function definition ends here
%% MATLAB code to run function
clc;
close all;
clear all;
%%
disp('Solution for a');
A=[4 0 -7 -7
-6 1 11 9
7 -5 10 19
-1 2 3 -1];
disp('Using inverses function');
inv_A=inverses(A);
disp(inv_A);
disp('Using MATLAB function inv()');
inv_matlab=inv(A);
disp(inv_matlab);
%% Solution for b
disp('Solution for b');
A=[1 -3 2 -4
-3 9 -1 5
2 -6 4 -3
-4 12 2 7];
disp('Using inverses function');
inv_A=inverses(A);
disp(inv_A);
disp('Using MATLAB function inv()');
inv_matlab=inv(A);
disp(inv_matlab);
%% Solution for c
disp('Solution for c');
A=magic(5);
disp('Using inverses function')
inv_A=inverses(A);
disp(inv_A);
disp('Using MATLAB function inv()');
inv_matlab=inv(A);
disp(inv_matlab);
%% Solution for d
disp('Solution for d');
A=magic(4);
disp('Using inverses function')
inv_A=inverses(A);
disp(inv_A);
disp('Using MATLAB function inv()')
inv_matlab=inv(A);
disp(inv_matlab);
disp(' The inv function fails if determinant of matrix is close to zero. Matrix is close to singular or badly scaled. Results may be inaccurate')
OUTPUT:
Solution for a
Using inverses function
-19 -14 0 7
-549 -401 -2 196
267 195 1 -95
-278 -203 -1 99
Using MATLAB function inv()
-19.0000 -14.0000 -0.0000 7.0000
-549.0000 -401.0000 -2.0000 196.0000
267.0000 195.0000 1.0000 -95.0000
-278.0000 -203.0000 -1.0000 99.0000
Solution for b
Using inverses function
A is not Invertible
Using MATLAB function inv()
Warning: Matrix is singular to working precision.
> In Run_inverse_func at 27
Inf Inf Inf Inf
Inf Inf Inf Inf
Inf Inf Inf Inf
Inf Inf Inf Inf
Solution for c
Using inverses function
-0.0049 0.0512 -0.0354 0.0012 0.0034
0.0431 -0.0373 -0.0046 0.0127 0.0015
-0.0303 0.0031 0.0031 0.0031 0.0364
0.0047 -0.0065 0.0108 0.0435 -0.0370
0.0028 0.0050 0.0415 -0.0450 0.0111
Using MATLAB function inv()
-0.0049 0.0512 -0.0354 0.0012 0.0034
0.0431 -0.0373 -0.0046 0.0127 0.0015
-0.0303 0.0031 0.0031 0.0031 0.0364
0.0047 -0.0065 0.0108 0.0435 -0.0370
0.0028 0.0050 0.0415 -0.0450 0.0111
Solution for d
Using inverses function
0 -0.1544 0.1838 0.0294
0 0.8162 -0.2574 -0.4412
0 -0.7206 0.1912 0.4706
1.0000 3.0000 -3.0000 -1.0000
Using MATLAB function inv()
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.306145e-17.
> In Run_inverse_func at 45
1.0e+14 *
0.9382 2.8147 -2.8147 -0.9382
2.8147 8.4442 -8.4442 -2.8147
-2.8147 -8.4442 8.4442 2.8147
-0.9382 -2.8147 2.8147 0.9382
Explanation for different result in case of magic(4) :
The inv function fails if determinant of matrix is close to zero. Matrix is close to singular or badly scaled. Results may be inaccurate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.