# matlab gives wrong answer to determinant of a 3x3 matrix

23 views (last 30 days)
Ozan Mirzanli on 22 Mar 2020
Commented: Subhamoy Saha on 23 Mar 2020
A=[1,4,7;11,5,-1;0,2,4]
det(A)
= -3.3509e-15
which is wrong and the result is actually equal to 0.
why does matlab calculate it wrong?

Ozan Mirzanli on 22 Mar 2020
thank you very much John!
John D'Errico on 22 Mar 2020
As a followup, why are determinants bad things? Because they have nasty scaling probems. Consider the following determinants:
A = eye(1000);
B = 3*A;
C = A/3;
det(A)
ans =
1
det(B)
ans =
Inf
det(C)
ans =
0
While this is a bit of an extreme example, note that all three matrices are diagonal matrices, and are about as non-singular as you can possibly imagine. However, simply by multiplying the matrix by a small constant, I can make the determinants as large or as small as I want to, even overflowing or underflowing the dynamic range of a double.
The point is, you should never test for a determinant being zero, and even testing for small determinants is a dangerous thing, because I can make them as arbitrarily small or large as I wish. And since that arbitrary scaling of the matrix has absolutely nothing to do with the singularity status of said matrix, using a determinant is a bad idea. Yet, we are taught to use determinants to do exactly that.
Instead, there are far safer tools to infer if a matrix is singular or nearly so, starting with svd, cond, rank, rref, qr, etc.
Subhamoy Saha on 23 Mar 2020
Dear @John, I completely agree with you. It's my fault. Actually I meant to check the tolerance and yes rounding is irrelevant here. However, in context of the question, I must say the refernces provided by @the cyclist and @Samuele are relevant because the question asked why matlab gives error and not why the use of determinant=0 should be avoided. But yes, whatever you suggested is valuable and I accept it was not known to me before. Thank you.

the cyclist on 22 Mar 2020
Edited: the cyclist on 22 Mar 2020
MATLAB gives the correct result to within the limits of double-precision floating-point arithmetic.
This is a very common question on the forum. You could start reading about it in this answer.

#### 1 Comment

Ozan Mirzanli on 22 Mar 2020
thank you

Samuele Sandrini on 22 Mar 2020
To calculate the determinant Matlab doesn't use an analytical formula but to calculate it in a simpler way, first it passes from the LU factorization which is susceptible to floating-point round-off errors (Limitations and Algorithms of function det).
Note:
The LU factorization allows to write the matrix A as the product of two matrices (L, U) where L is lower triangular and U is upper triangular.
Therefore:
in this way it is easier calculate the determinant because L and U are triangular.
Alternative:
If you want to verify that the matrix A is singular you can use the condition number (cond) and if it assumes very high values it means that the matrix is "close" to the singularity.
Otherwise, if you need to invert the matrix A you can use the operator "\" which uses more efficient methods (you could reading about it in this answer) .