# MATLAB: Is det(x) better than rcond(x) in determining non-singularity here

analyticaldetdeterminanteiginvMATLABprecisionrcondsingular

The code below demonstrates the following problem:
Analytically, the matrix x is NOT singular [1]. Matlab believes it is singular due to limits on floating point precision. Documentation dictates that using det(x) to determine if a matrix is singular is poor practice, and using rcond(x) (for instance) is better [2]. However, in this case, det(x) yields the analytical solution of the determinant. Why does det(x) accurately determine that x is not singular while rcond(x) does not?
``a = -1.3589e-10;b = -1.7108e-9;c = 12.5893;d = -1e11;x = [a b; c d];analytical_determinant = a*d - b*c; %[3]matlab_determinant = det(x);eigens = eig(x);eigen_determinant = eigens(1)*eigens(2); %[4]rcond_result = rcond(x);disp('Let''s try to take an inverse:')inv(x);fprintf('rcond result: %f\nanalytical det: %f\ndet(x) result: %f\ndeterminant from eig: %f\n',rcond_result,analytical_determinant,matlab_determinant,eigen_determinant);``

Let me suggest that you misunderstand det, and why it is a terribly poor tool to use to learn if a matrix is singular or not. Yes, we are taught to use det by teachers, in school. Hey it works there, so why not use it in practice? The problem is those same teachers stopped short of explaining the real issues. I've often said that the use of det is a bad meme, one that just keeps propagating forever. One person learns about it, so they teach others. That it is a bad idea? Nobody ever told them. So det lives on - IT'S ALIVE! (With my apologies to the movie where that line was used. Thanks Mel.)
``a = -1.3589e-10;b = -1.7108e-9;c = 12.5893;d = -1e11;x = [a b; c d];``
So, is the 2x2 matrix singular or not? It is NUMERICALLY singular, thus in floating point double precision arithmetic, it can be risky to use a numerically singular matrix to solve a system of linear equations reliably, even though an inverse exists in theory.
A big problem with det is that it is sensitive to scaling. What result would indicate to you that a matrix is singular based on the determinant? Remember that a numerically computed determinant will virtually NEVER be exactly zero. For example, consider this matrix, A:
``A = rand(4,5);A = [A;sum(A,1)];AA =     0.031833      0.82346     0.034446       0.7952      0.64631      0.27692      0.69483      0.43874      0.18687      0.70936     0.046171       0.3171      0.38156      0.48976      0.75469     0.097132      0.95022      0.76552      0.44559      0.27603      0.45206       2.7856       1.6203       1.9174       2.3864      det(A)ans =   3.0549e-18``
Is A singular? It darn tootin is! The last row of A is the sum of the other 4 rows. But wait a second, det(A) is not returned as zero. Wwwwhhhhaaaatttt happened?
And, if you claim that 3e-18 is close enough to zero, then just multiply A by 10000.
``det(10000*A)ans =       5005.1``
Gosh, 10000*A is surely not singular based on what det just told us. But I thought that we decided A was singular? Mathematically, A is indeed singular. We know that from theory. Theory would never lie, would it?
Or try this:
``A = eye(1200);det(A)ans =     1``
``det(2*A)ans =   Infdet(A/2)ans =     0``
``A = magic(4)A =    16     2     3    13     5    11    10     8     9     7     6    12     4    14    15     1     rank(A)ans =     3     cond(A)ans =    8.148e+16``
``det(sym(A))ans =0det(A)ans =  -1.4495e-12``
``[~,U] = lu(A);prod(diag(U))ans =  -1.4495e-12``