MATLAB returns a wrong value of determinant.

28 次查看(过去 30 天)
Consider a 2x2 Matrix X whose entries are
5 25
7 35
By hand calculations, we know that the determinant of the Matrix is 0. How come when MATLAB computes this, it gives a value of 3.8858e-15? Here's the Code snippet.
Using also MATLAB R2021b
X =[5 25; 7 35];
D = det(X)

采纳的回答

Paul
Paul 2021-11-10
The reason det() doesn't return zero for this matrix is because the floating point errors in the computation of the determinant. If det() computed the determinant using the textbook formula, the result would, of course, be zero.
X =[5 25; 7 35];
D0 = X(1,1)*X(2,2) - X(2,1)*X(1,2)
D0 = 0
But det() uses a more general algorithm as shown on its doc page and repeated here. This algorithm like any other is subject to numerical error
D = det(X)
D = 3.8858e-15
[L,U]=lu(X);
s = det(L);
D1 = s*prod(diag(U));
[D D1]
ans = 1×2
1.0e+-14 * 0.3886 0.3886
isequal(D,D1)
ans = logical
1

更多回答(1 个)

Jon
Jon 2021-11-10
There will always be some tolerance in numerical methods compared with exact solutions. 3.88858e-15 is considered very small, and effectively can be treated as zero. It is important to realize this whenever working with numerical methods particularly when testing for certain conditions. You should always define a threshold for what is considered close enough to zero. One useful value you can get to help you scale this is the machine precision, which you can get using MATLAB's eps function. You can then define zero as being some multiple of the machine precision.
  1 个评论
Rik
Rik 2021-11-10
The reason behind this is that Matlab stores data with finite memory, which might not be enough to yield the mathematically correct answer.
The reason you should consider a value of 1e-15 as 0 is precisely this.
det([5 25;7 35]*1e-15)
ans = 3.7397e-44
Here you see the exact same thing. I personally treat every value 15 orders of magnitude smaller than the input numbers as 0. If you need more precision, you shouldn't be using the standard tools.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by