Accelerating the pace of engineering and science

# det

Matrix determinant

## Description

example

d = det(A) returns the determinant of square matrix A.

## Examples

expand all

### Calculate Determinant of Matrix

Create a 3-by-3 square matrix, A.

`A = [1 -2 4; -5 2 0; 1 0 3]`
```A =

1    -2     4
-5     2     0
1     0     3```

Calculate the determinant of A.

`d = det(A)`
```d =

-32```

The determinant of A is -32.

### Determine if Matrix Is Singular

Examine why the determinant is not an accurate measure of singularity.

Create a 10-by-10 matrix by multiplying an identity matrix, eye(10), by a small number.

`A = eye(10)*0.0001;`

The matrix A has very small entries along the main diagonal. However, A is not singular, because it is a multiple of the identity matrix.

Calculate the determinant of A.

`d = det(A)`
```d =

1.0000e-40```

The determinant is extremely small. A tolerance test of the form abs(det(A)) < tol is likely to flag this matrix as singular. Although the determinant of the matrix is close to zero, A is actually not ill conditioned. Therefore, A is not close to being singular. The determinant of a matrix can be arbitrarily close to zero without conveying information about singularity.

To investigate if A is singular, use either the cond or rcond functions.

Calculate the condition number of A.

`c = cond(A)`
```c =

1
```

The result confirms that A is not ill conditioned.

### Compute Determinant of Inverse of Ill-Conditioned Matrix

Examine how to calculate the determinant of the matrix inverse A^(-1), for an ill-conditioned matrix A, without explicitly calculating A^(-1).

Create a 10-by-10 Hilbert matrix, A.

`A = hilb(10);`

Find the condition number of A.

`c = cond(A)`
```c =

1.6025e+13```

The large condition number suggests that A is close to being singular, so calculating inv(A) might produce inaccurate results. Therefore, the inverse determinant calculation det(inv(A)) is also inaccurate.

Calculate the determinant of the inverse of A by exploiting the fact that $\text{det}\left({A}^{-1}\right)\text{\hspace{0.17em}}=\frac{1}{\mathrm{det}\left(A\right)}\text{\hspace{0.17em}}\text{\hspace{0.17em}}.$

`d1 = 1/det(A)`
```d1 =

4.6202e+52```

This method avoids computing the inverse of the matrix, A.

Calculate the determinant of the exact inverse of the Hilbert matrix, A, using invhilb. Compare the result to d1 to find the relative error in d1.

```d = det(invhilb(10));
relError = abs(d1-d)/abs(d)```
```relError =

1.1462e-04```

The relative error in d1 is reasonably small. Avoiding the explicit computation of the inverse of A minimizes it.

For comparison, also calculate the determinant of the inverse of A by explicitly calculating the inverse. Compare the result to d to see the relative error.

```d2 = det(inv(A));
relError2 = abs(d2-d)/abs(d)```
```relError2 =

1.2427e+08```

The relative error in the calculation of d2 is many orders of magnitude larger than that of d1.

### Find Determinant of Singular Matrix

Examine a matrix that is exactly singular, but which has a large nonzero determinant. In theory, the determinant of any singular matrix is zero, but because of the nature of floating-point computation, this ideal is not always achievable.

Create a 13-by-13 diagonally dominant singular matrix, A.

```A = diag([24 46 64 78 88 94 96 94 88 78 64 46 24]);
S = diag([-13 -24 -33 -40 -45 -48 -49 -48 -45 -40 -33 -24],1);
A = A + S + rot90(S,2)
```
```A =

24   -13     0     0     0     0     0     0     0     0     0     0     0
-24    46   -24     0     0     0     0     0     0     0     0     0     0
0   -33    64   -33     0     0     0     0     0     0     0     0     0
0     0   -40    78   -40     0     0     0     0     0     0     0     0
0     0     0   -45    88   -45     0     0     0     0     0     0     0
0     0     0     0   -48    94   -48     0     0     0     0     0     0
0     0     0     0     0   -49    96   -49     0     0     0     0     0
0     0     0     0     0     0   -48    94   -48     0     0     0     0
0     0     0     0     0     0     0   -45    88   -45     0     0     0
0     0     0     0     0     0     0     0   -40    78   -40     0     0
0     0     0     0     0     0     0     0     0   -33    64   -33     0
0     0     0     0     0     0     0     0     0     0   -24    46   -24
0     0     0     0     0     0     0     0     0     0     0   -13    24```

A is singular because the rows are linearly dependent. For instance, sum(A) produces a vector of zeros.

Calculate the determinant of A.

`d = det(A)`
```d =

1.059686712222177e+05```

The determinant of A is quite large despite the fact that A is singular. In fact, the determinant of A should be exactly zero! The inaccuracy of d is due to an aggregation of round-off errors in the MATLAB® implementation of the LU decomposition, which det uses to calculate the determinant. This result demonstrates a few important aspects of calculating numeric determinants. See the Limitations section for more details.

## Input Arguments

expand all

### A — Input matrixsquare numeric matrix

Input matrix, specified as a square numeric matrix.

Data Types: single | double
Complex Number Support: Yes

## Limitations

Avoid using det to examine if a matrix is singular because of the following limitations. Use cond or rcond instead.

LimitationResult

The magnitude of the determinant is typically unrelated to the condition number of a matrix.

The determinant of a matrix can be arbitrarily large or small without changing the condition number.

det uses the LU decomposition to calculate the determinant, which is susceptible to floating-point round-off errors.

The determinant calculation is sometimes numerically unstable. For example, det can produce a large-magnitude determinant for a singular matrix, even though it should have a magnitude of 0.

expand all

### Algorithms

det computes the determinant from the triangular factors obtained by Gaussian elimination with the lu function.

```[L,U] = lu(X)
s =  det(L)      % This is always +1 or -1
det(X) = s*prod(diag(U))```