필터 지우기
필터 지우기

matlab gives wrong answer to determinant of a 3x3 matrix

조회 수: 10 (최근 30일)
Ozan Mirzanli
Ozan Mirzanli 2020년 3월 22일
댓글: Subhamoy Saha 2020년 3월 23일
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?
  댓글 수: 6
John D'Errico
John D'Errico 2020년 3월 22일
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
Subhamoy Saha 2020년 3월 23일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2020년 3월 22일
편집: the cyclist 2020년 3월 22일
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.

Samuele Sandrini
Samuele Sandrini 2020년 3월 22일
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) .

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by