MATLAB returns a wrong value of determinant.

8 views (last 30 days)
Jem
Jem on 10 Nov 2021
Answered: Paul on 10 Nov 2021
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)

Accepted Answer

Paul
Paul on 10 Nov 2021
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

More Answers (1)

Jon
Jon on 10 Nov 2021
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 Comment
Rik
Rik on 10 Nov 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!