MATLAB: Error while calculating a matrix determinant

determinantmatrixmatrix determinantwrong resultwrong results

I encountered a problem while calculating a matrix determinant.
Using the following code: det([-3,1,1;-1,0,1;1,1,-3])
matlab say: ans=4.4409e-16
while the correct determinant is 0. what should I do to fix this?

Best Answer

1. NEVER use the matrix determinant for any computation to determine the rank of a matrix. (Use rank or cond for that.)
1a. Never use the matrix determinant for any computation, unless you understand enough about mathematics and numerical linear algebra to know why you should virtually never use the determinant, and when it might be ok.
2. Learn about floating point arithmetic, and understand why you should never trust those bits down at the LSB level. A number on the order of 1e-16 is exactly that. It is zero in double precision arithmetic.
3. You cannot "fix" the problem you perceive, except by learning {1,1a,2} from above. Yes,you can use symbolic tools to compute an "exact" value for the determinant. But they will be EXTREMELY slow for such a result, especially as the matrix size goes only slightly higher.
Yes, it is true that you were taught in some basic math class about the determinant of a matrix. Sadly, the person who taught you was wrong, in that either they did not understand the above, or they forgot to teach you those facts at the same time as they taught you about the determinant. Sadly, many teachers only know what they see in a book, and what they themselves were taught. So these memes propagate. In this case, the meme is that you can use the determinant.
Related Question