MATLAB Answers

## Matlab gives wrong determinant value of 2x2 Matrix without warning.

Asked by Nitin Chhabra

### Nitin Chhabra (view profile)

on 19 Mar 2012
Hi,
When I am taking the determinant of 2x2 reciprocal matrix with matlab ( which should be 1 ) , matlab is giving wrong results without any warning for overflow. Here is what I am doing :
m =
63245986 102334155
102334155 165580141
>> det(m)
ans =
1.5249
>> m(1)*m(4)-m(2)*m(3)
ans =
2
If I mannually do the calculation , I am getting the right result ie ans=1 . Please check and tell the reason for the same.

#### 0 Comments

Sign in to comment.

## 5 Answers

Answer by Derek O'Connor

### Derek O'Connor (view profile)

on 20 Mar 2012

@Nitin,
The "issue" here is not round-off. It is the ill-condition of your matrix. Floating point arithmetic and its rounding merely certify that your matrix is ill-conditioned; it does not cause your matrix to be ill-conditioned. Even if you use infinite precision (exact) arithmetic, your matrix is still ill-conditioned. There is no floating point "work-around".
Your calculation does not "suffer from large roundoff error". The large (forward) error is caused by a small round-off error which is then magnified by the huge condition number: Ef = Cond(A)*Er.
I would follow Nick Trefethen's advice and ask a different question (use a different matrix):
If the answer is highly sensitive to perturbations, you have probably asked the wrong question.

#### 0 Comments

Sign in to comment. ### Andreas Goser (view profile)

Answer by Andreas Goser

### Andreas Goser (view profile)

on 19 Mar 2012

I can explain, but can't tell what do do else. If you look at
m(1)*m(4)
ans =
1.047227927956403e+16
m(2)*m(3)
ans =
1.047227927956402e+16
Then you see those numbers are large and relatively close to each other. What you see is simply a numerical effect.

Titus Edelhofer

### Titus Edelhofer (view profile)

on 19 Mar 2012
To add one point which also helps the explanation: from a numerical point of view this matrix is "bad":
norm(m)
ans =
2.2883e+08
Any numerical calculation, not only det, will suffer from large roundoff error.
Titus
Titus Edelhofer

### Titus Edelhofer (view profile)

on 19 Mar 2012
Second comment: to get the exact result you will need to compute the determinant symbolically:
M = sym(m)
M =
[ 63245986, 102334155]
[ 102334155, 165580141]
det(M)
ans =
1
Aldin

### Aldin (view profile)

on 19 Mar 2012
Yes, you have right!

Sign in to comment.

Answer by Aldin

### Aldin (view profile)

on 19 Mar 2012

It depends of your MATLAB version. My version is MATLAB 7.9.0 (R2009b) - worked a charm. By me the result of matrix is 2.

Andreas Goser

### Andreas Goser (view profile)

on 19 Mar 2012
Which may be more the effect of 32 bit vs. 64 bit MATLAB or a different processor or a different BLAS library...

Sign in to comment.

Answer by Derek O'Connor

### Derek O'Connor (view profile)

on 19 Mar 2012

The products above are too big to fit in 32-bit integers. You can get the correct result by switching to 64-bit integers:
>> m64 = int64(A)
>> detm64 = m64(1)*m64(4)-m64(2)*m64(3)
detm64 = 1
Look at the L-U decomposition of A:
>> [L U P] =lu(A)
L =
1.000000000000000e+000 0
6.180339887498949e-001 1.000000000000000e+000
U =
1.023341550000000e+008 1.655801410000000e+008
0 -1.490116119384766e-008
P =
0 1
1 0
This shows that U(2,2) is numerically zero relative to U(1,1) and U(1,2). Thus U is numerically singular, and so is A.
Now look at the lower bound estimate of the 1-norm condition of A:
lbcond1A = condest(A) = 4.707074327130931e+016
This shows that A is very ill-conditioned. There is no hope of getting accurate results with double precision because log10(condest(A)) is about 17, the number of digits of precision lost when calculating the solution of Ax = b.
This example shows that there is no connection between det(A) and the condition of a matrix.

#### 0 Comments

Sign in to comment.

Answer by Nitin Chhabra

### Nitin Chhabra (view profile)

on 20 Mar 2012

Hi Andreas/Titus/Derek,
You are correct that values are numerically zero relative to other values and I am getting roundoff errors. But I require some work-around for this round off errors ( numerical issues ) in case of complex matix(a+jb).
with regards,
Nitin

#### 0 Comments

Sign in to comment.