When I used matlab to calculate the determinant of the matri
When I used matlab to calculate the determinant of the matrix a, which is magic(4), in two different ways, I got two different determinants. Could someone explain why there are two determiannts for the same matrix in this situation? The first way I use to compute the determinant is det(a). The second way is that I created two functions (cofactor and determine). Cofactor function calculates the cofactors of the matrix a. Determine function calculates the determinant of a by using cofactors obtained from the cofactor function.
The determinant obtained from det(a) is -1.4495e-12. The determinant obtained from functions is 3.1832e-12. Thanks~
a=magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> det(a)
ans =
-1.4495e-12
Cofactor function:
function C=cofactor(a)
[n,n]=size(a);
for i=1:n;
for j=1:n;
A=a;
A(i,:)=[];
A(:,j)=[];
C(i,j)=(-1).^(i+j)*det(A);
end
end
Determine function:
function D=determine(a,C)
[n,n]=size(a);
i=1;
while i<=n
d=0;
for j=1:n;
dd=a(i,j)*C(i,j);
d=d+dd;
end
D1(i,1)=d;
i=i+1;
end
j=1;
while j<=n
d=0;
for i=1:n;
dd=a(i,j)*C(i,j);
d=d+dd;
end
d2(1,j)=d;
j=j+1;
end
D2=transpose (d2);
D12=[D1 D2];
[m,n]=size(D12);
i=1;j=1;
while i<=m && j<=n
if abs (D12(i,j)-D12(1,1))<10^(-7)
D=D1(1);
else
D=[];
disp(\'There is a problem with my code!\');
break;
end
i=i+1;
j=j+1;
end
>> C=cofactor(a)
C =
1.0e+03 *
-0.1360 -0.4080 0.4080 0.1360
-0.4080 -1.2240 1.2240 0.4080
0.4080 1.2240 -1.2240 -0.4080
0.1360 0.4080 -0.4080 -0.1360
>> D=determine(a,C)
D =
3.1832e-12
Solution
your cofactor function is correct but in the determine function after d=d+dd then write dd=0 other wise it assume the previous value. I revised the code...
function D=determine(a,C)
[n,n]=size(a);
i=1;
while i<=n
d=0;
for j=1:n;
dd=a(i,j)*C(i,j);
d=d+dd;
dd =0; %(This is the edited line)
end
Now you can run the program for odd n (>=3) and you get expected result....for other value of n you have to update the program..like add some condition as matrix determinant is totally dependent of its rank. and you know that..
For n odd, the rank of the magic square is n. For n divisible by 4, the rank is 3. For n even but not divisible by 4, the rank is n/2 + 2. so rank of magic(4) is 3 so determinant must e zero.
you add the condition if rank(A) = length(A) then your program reasult and det(a) is correct. for other n you add the condition in your program if rank(a)<length(a) then determinant of a is zero. But matlab is geven very very small value.
So update your program and run it and I hope that you will get your reasult. If not please tell me I upload the full code.
%%%%.............................%%%%....UPDATE answer................%%%%........................................
I given the MATLAB code below..Please see the code carefully.....and chack the reasult but remember that matlab is not calculate the determinant in this way...
%%%code......................................%%.........
function C=cofactor(a)
format long;
[n,n]=size(a);
for i=1:n
for j=1:n
A = a;
A(i,:)=[];
A(:,j)=[];
C(i,j)=((-1).^(i+j))*det(A);
end
end
..................................................................
function D=determine(a,C)
format long;
[n,n]=size(a);
i = 1;
d=0;
det(a)
for j = 1:n
dd = a(i,j)*C(i,j);
d=d+dd;
dd=0;
end
end
%%%...................OUTPUT....................%%
>> a = magic(3);
>> rank(a)
ans =
3
>> det(a)
ans =
-360
>> C = cofactor(a);
>> D = determine(a,C)
ans =
-360
Output argument \"D\" (and maybe others) not assigned during call to \"determine\".
>> a = magic(4);
>> rank(a)
ans =
3
>> det(a)
ans =
-1.449507180950605e-12
>> C = cofactor(a);
>> D = determine(a,C)
ans =
-1.449507180950605e-12
Output argument \"D\" (and maybe others) not assigned during call to \"determine\".
>> a = magic(6);
>> rank(a)
ans =
5
>> det(a)
ans =
-6.899568916196585e-09
>> C = cofactor(a);
>> D = determine(a,C)
ans =
-6.899568916196585e-09
Output argument \"D\" (and maybe others) not assigned during call to \"determine\".
>> a = magic(5);
>> rank(a)
ans =
5
>> det(a)
ans =
5.070000000000001e+06
>> C = cofactor(a);
>> D = determine(a,C)
ans =
5.070000000000001e+06
Output argument \"D\" (and maybe others) not assigned during call to \"determine\".
%%%..............
But remember that output of this code totally matlab version dependent.....
Please read the passage carefually below and you understood that how matlab calculate the Determinant...
%%%passage.................................
In theory, one can simply test if the determinant of your matrix is zero. Thus
As it turns out, this matrix is indeed singular, so there is a way to write a row of M as a linear combination of the other rows (also true for the columns.) But we got a value that was not exactly zero from det. Is it really zero, and MATLAB just got confused? Why is that? The symbolic determinant is truly zero.
As it turns out, computation of the determinant is a terribly inefficient thing for larger arrays. So a nice alternative is to use the product of the diagonal elements of a specific matrix factorization of our square array. In fact, this is what MATLAB does inside det itself for non-symbolic inputs.
See that the diagonal elements of L are ones, but U has non-zero diagonal elements. And there are nice properties about the determinant of a product of matrices, as well as the determinant of upper or lower triangular matrices.
See that we got the same answer. Since a LU factorization is reasonably fast even for large arrays, it is a nice way to compute the determinant. The problem is, it uses floating point arithmetic. So those diagonal elements of U are real, floating point numbers. When we take the product, we get a result that is not exactly zero. That last element was just slightly non-zero.
%%%..........I given some link if you see it you understood clearly.....
http://stackoverflow.com/questions/13145948/how-to-find-out-if-a-matrix-is-singular
https://in.mathworks.com/matlabcentral/answers/17297-determinant-of-singular-matrix-is-non-zero




