** Create a function function C= cofactor ( a ) The function has to generate ( n
ID: 3209791 • Letter: #
Question
** Create a function
function C=cofactor(a)
The function has to generate (n-1) x (n-1) matrices Aij (i=1:n, j=1:n). Each Aij is obtained from a by deleting row i and column j. The output matrix is an nxn matrix
C=[C(i,j)](i,j=1:n),
whose entries are the cofactors calculated by the formulas C(i,j)=(-1)i+jdetAij.
Note: You will be using the MATLAB built-in function det when computing the determinants of (n-1) x (n-1) matrices Aij.
Step 2:
** Create a function that will compute the determinant D of a by using cofactors of a, which are the entries of the matrix C:
function D=determine(a, C)
The function has to calculate the cofactor expansion across each row of a – these numbers should be the entries of the vector D1; and it has to calculate the cofactor expansion down every column of a – these numbers should be the entries of the vector D2. According to the theory, all entries of the vectors D1 and D2 must be equal to the same number. You have to verify that it is the case. I suggest taking the first entry of the vector D1, that is D1(1), as a reference, and consider the absolute values of the differences between each entry of vectors D1, D2 and the number D1(1). If each of the values is zero (for MATLAB, it is less than 10^(-7)), the code assigns (and outputs) D = D1(1); otherwise, the function returns an empty value, D = [ ], and a message “Oh no! There must be a problem with my code!”
Hint: You could use for loop and while loop within your code (the command break stops executing the while loop, if needed).
Step 3:
**Create a function that will compute the matrix B=a-1 for an invertible matrix a:
function B=inverse(a, C, D)
The function has to calculate the inverse B of an invertible matrix a by means of the formula
B=(1/D)*transpose(C),
where D is the determinant of a, and C is the cofactors matrix. If a matrix a is not invertible, which has to be verified by using the function rank(a) rather than det(a) (% explain why!), the output has to be an “empty_matrix”.
**Type the functions cofactor, determine, inverse in your diary file.
**For each matrix a in parts (a)-(e) below, run the functions in the following order:
(1) C=cofactor(a)
(2) D=determine(a, C) (which works with the output C from the previous function)
**At this step, also run a MATLAB function det(a).
% If the output for det is different from the output for determine, find out why and write a comment in your diary file.
(3) B=inverse(a, C, D) (which works with the outputs C and D from the previous functions)
**At this step, also run a MATLAB function inv(a) for an invertible matrix a.
If the output for inv is different from the output for inverse, find out why and write a comment in your diary file.
**Use the command
format rat
and
**run the functions that you created on these matrices in the order indicated in (1)-(3):
(a) a = diag([1,2,3,4,5])
(b) a = ones(4)
(c) a = magic(3)
(d) a = magic(4)
(e) a = hilb(5)
Note: for part (e), after you typed a = hilb(5), change the format rat to the default in order to have your outputs displayed. That is, **type
format, format compact
and then run the functions cofactor, determine, and inverse on the matrix a = hilb(5).
Explanation / Answer
ANSWER:
%%%% Matlab functions
function [C]=cofactor(a)
[m n]=size(a);
for i=1:m
for j=1:n
e=1;
for x=1:m
f=1;
for y=1:n
if (x~=i && y~=j)
A(e,f)=a(x,y);
f=f+1;
if(f==n)
e=e+1;
end
end
end
end
C(i,j)=(-1)^(i+j)*det(A);
end
end
end
%%%%%
function [D] = determine(a,C)
[m,n]=size(a);
D=0;
for i=1:m
D1(i)=sum(a(i,:).*C(i,:));
end
for j=1:n
D2(j)=sum(a(:,j).*C(:,j));
end
for k=1:m
if D1(k)~=D2(k)
disp('Something wrong with my code ');
break;
end
end
if k==m
D=D1(1);
end
end
%%%%%
function B = inverse(a,C,D)
[m n]=size(a);
if (rank(a)==m)
B=C'/D;
else
B=[];
end
end
%%%%%
%%% main programe
clc;
clear all;
close all;
format short
format compact;
%%% a)
a=diag([1 2 3 4 5]);
C=cofactor(a);
D=determine(a,C);
disp('inverse by user defined method' )
B=inverse(a,C,D)
disp('inverse by built in cammand inv(a) ');
inv(a)
OUTPUT:
inverse by user defined method
B =
1.0000 0 0 0 0
0 0.5000 0 0 0
0 0 0.3333 0 0
0 0 0 0.2500 0
0 0 0 0 0.2000
inverse by built in cammand inv(a)
ans =
1.0000 0 0 0 0
0 0.5000 0 0 0
0 0 0.3333 0 0
0 0 0 0.2500 0
0 0 0 0 0.2000
>>
%%% b)
%%% b)
a=ones(4);
C=cofactor(a);
D=determine(a,C);
disp('inverse by user defined method' )
B=inverse(a,C,D)
OUTOUT:
inverse by user defined method
B =
[]
%%% c)
a=magic(3);
C=cofactor(a);
D=determine(a,C);
disp('inverse by user defined method' )
B=inverse(a,C,D)
disp('inverse by built in cammand inv(a) ');
inv(a)
OUTPUT:
inverse by user defined method
B =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
inverse by built in cammand inv(a)
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
%%%%d)
%%% d)
a=magic(4);
C=cofactor(a);
D=determine(a,C);
disp('inverse by user defined method' )
B=inverse(a,C,D)
OUTPUT:
Something wrong with my code
inverse by user defined method
B =
[]
%%%e)
%%% e)
a=hilb(5);
C=cofactor(a);
D=determine(a,C);
disp('inverse by user defined method' )
B=inverse(a,C,D)
disp('inverse by built in cammand inv(a) ');
inv(a)
OUTPUT:
Something wrong with my code
inverse by user defined method
B =
Inf -Inf Inf -Inf Inf
-Inf Inf -Inf Inf -Inf
Inf -Inf Inf -Inf Inf
-Inf Inf -Inf Inf -Inf
Inf -Inf Inf -Inf Inf
inverse by built in cammand inv(a)
ans =
1.0e+05 *
0.0002 -0.0030 0.0105 -0.0140 0.0063
-0.0030 0.0480 -0.1890 0.2688 -0.1260
0.0105 -0.1890 0.7938 -1.1760 0.5670
-0.0140 0.2688 -1.1760 1.7920 -0.8820
0.0063 -0.1260 0.5670 -0.8820 0.4410
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.