Linear Algebra Project Exercise in MATLAB Thank you! Exercise 7 (6 points) Diffi
ID: 3209627 • Letter: L
Question
Linear Algebra Project Exercise in MATLAB
Thank you!
Exercise 7 (6 points) Difficulty: Hard Step 1: **Create a function function C-cofactor(a) The function has to generate (n-1) x (n-1) matrices 4, ( 1: n, j = 1 : n). Each 4, is obtained from a by deleting row i and column j. The output matrix is an nxn matrix whose entries are the cofactors calculated by the formulas C(i,j)-(-1)"j det A Note: You will be using the MATLAB built-in function det when computing the determinants of (n-1)×(n-1) matrices Aj. 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)Explanation / 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.