f

#### determinant of magic square in matlab 2010a

```I am newer to MATLAB.
I followed getting started, and calculate the det of 4*4 magic square, it gives finite value instead of 0. Why?

>> A=magic(4);
>> det(A)

ans =

-1.4495e-012
```
 0
Wei
4/26/2010 8:27:05 AM
comp.soft-sys.matlab 211266 articles. 19 followers. lunamoonmoon (257) is leader.

4 Replies
801 Views

Similar Articles

[PageSpeed] 21

```"Wei " <weitong@ustc.edu> wrote in message <hr3iop\$mtm\$1@fred.mathworks.com>...
> I am newer to MATLAB.
> I followed getting started, and calculate the det of 4*4 magic square, it gives finite value instead of 0. Why?
>
> >> A=magic(4);
> >> det(A)
>
> ans =
>
>  -1.4495e-012

Considering the magnitude of the magic square components this is numerically equivalent to zero. Its due to a rounding error somewhere.
```
 0
Thomas
4/26/2010 8:59:05 AM
```Yes, I know it  (-1.4495e-012)  is somehow "zero".
But, I tried some previous version, e.g., 2009a, 7.1, and 6.1, and they all gave "0" answer as the Help says.
So, I wish to know why it happens to 2010a, and where can I change it.

Thanks.

"Thomas Britton" <benjamin.britton@materials.ox.ac.remove.uk> wrote in message <hr3kkp\$nt4\$1@fred.mathworks.com>...
> "Wei " <weitong@ustc.edu> wrote in message <hr3iop\$mtm\$1@fred.mathworks.com>...
> > I am newer to MATLAB.
> > I followed getting started, and calculate the det of 4*4 magic square, it gives finite value instead of 0. Why?
> >
> > >> A=magic(4);
> > >> det(A)
> >
> > ans =
> >
> >  -1.4495e-012
>
> Considering the magnitude of the magic square components this is numerically equivalent to zero. Its due to a rounding error somewhere.
```
 0
Wei
4/26/2010 9:18:04 AM
```"Wei " <weitong@ustc.edu> wrote in message <hr3loc\$5sa\$1@fred.mathworks.com>...
> Yes, I know it  (-1.4495e-012)  is somehow "zero".
> But, I tried some previous version, e.g., 2009a, 7.1, and 6.1, and they all gave "0" answer as the Help says.
> So, I wish to know why it happens to 2010a, and where can I change it.

Sigh. The determinant did not change, nor can you
make it magically become zero again.

This matrix is numerically singular. In fact, it is truly
singular. Of course, testing for singularity using the
determinant is a fool's game, best reserved for
textbooks written by authors who live in a fool's
world. The determinant is a terrible way to do that
test. Here I will define a fool's world as someplace
where the people understand (or care) nothing

Computation of the determinant of a matrix is a
nasty thing for large order matrices, at least if you
do it using the textbook way. This will get impossibly
inefficient for very reasonable size matrices. So
MATLAB uses a better scheme. One such scheme is
to recognize that the determinant is simply the
product of the eigenvalues of the matrix. Thus

M = magic(4);
ans =
-5.5968e-12

Note that this is a slightly different "zero" than
det gives. This is because det actually uses a better
scheme. det uses the product of the diagonal
elements of U, taken from the LU factorization

[L,U,P] = lu(M);
U
U =
16            2            3           13
0         13.5        14.25        -2.25
0            0      -1.8889       5.6667
0            0            0   3.5527e-15

prod(diag(U))
ans =
-1.4495e-12

Note that the result is not a true "zero". Get
used to living in the numerical world of floating
point arithmetic.

John
```
 0
John
4/26/2010 9:56:07 AM
```Thank you very much for exhaustive explanation of numerical algorithm.

Why do the previous versions give exact "zero"? Is the newest 2010a using a different algorithm for calculating determinant?

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hr3nvn\$44\$1@fred.mathworks.com>...
> "Wei " <weitong@ustc.edu> wrote in message <hr3loc\$5sa\$1@fred.mathworks.com>...
> > Yes, I know it  (-1.4495e-012)  is somehow "zero".
> > But, I tried some previous version, e.g., 2009a, 7.1, and 6.1, and they all gave "0" answer as the Help says.
> > So, I wish to know why it happens to 2010a, and where can I change it.
>
> Sigh. The determinant did not change, nor can you
> make it magically become zero again.
>
> This matrix is numerically singular. In fact, it is truly
> singular. Of course, testing for singularity using the
> determinant is a fool's game, best reserved for
> textbooks written by authors who live in a fool's
> world. The determinant is a terrible way to do that
> test. Here I will define a fool's world as someplace
> where the people understand (or care) nothing
>
> Computation of the determinant of a matrix is a
> nasty thing for large order matrices, at least if you
> do it using the textbook way. This will get impossibly
> inefficient for very reasonable size matrices. So
> MATLAB uses a better scheme. One such scheme is
> to recognize that the determinant is simply the
> product of the eigenvalues of the matrix. Thus
>
>   M = magic(4);
> ans =
>   -5.5968e-12
>
> Note that this is a slightly different "zero" than
> det gives. This is because det actually uses a better
> scheme. det uses the product of the diagonal
> elements of U, taken from the LU factorization
> (with pivoting) of your matrix.
>
> [L,U,P] = lu(M);
> U
> U =
>            16            2            3           13
>             0         13.5        14.25        -2.25
>             0            0      -1.8889       5.6667
>             0            0            0   3.5527e-15
>
> prod(diag(U))
> ans =
>   -1.4495e-12
>
> Note that the result is not a true "zero". Get
> used to living in the numerical world of floating
> point arithmetic.
>
> John
```
 0
Wei
4/27/2010 12:18:04 AM