Documentation

det

Matrix determinant

Syntax

Description

example

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

Examples

collapse all

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

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

Calculate the determinant ofA.

d = det(A)
d = -32

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 matrixAhas very small entries along the main diagonal. However,Aisnotsingular, because it is a multiple of the identity matrix.

Calculate the determinant ofA.

d = det(A)
d = 1.0000e-40

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

To investigate ifAis singular, use either thecondorrcondfunctions.

Calculate the condition number ofA.

c = cond(A)
c = 1

The result confirms thatAis not ill conditioned.

Examine how to calculate the determinant of the matrix inverseA^(-1), for an ill-conditioned matrixA, without explicitly calculatingA^(-1).

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

A = hilb(10);

Find the condition number ofA.

c = cond(A)
c = 1.6025 e + 13

The large condition number suggests thatAis close to being singular, so calculatinginv(A)might produce inaccurate results. Therefore, the inverse determinant calculationdet(inv(A))is also inaccurate.

Calculate the determinant of the inverse ofAby exploiting the fact that

d e t ( A - 1 ) = 1 d e t ( A )

d1 = 1/det(A)
d1 = 4.6201e+52

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

Calculate the determinant of the exact inverse of the Hilbert matrix,A, usinginvhilb. Compare the result tod1to find the relative error ind1.

d = det(invhilb(10)); relError = abs(d1-d)/abs(d)
relError = 1.2738e-04

The relative error ind1is reasonably small. Avoiding the explicit computation of the inverse ofAminimizes it.

For comparison, also calculate the determinant of the inverse ofAby explicitly calculating the inverse. Compare the result todto see the relative error.

d2 = det(inv(A)); relError2 = abs(d2-d)/abs(d)
relError2 = 1。1415e-04

The relative error in the calculation ofd2is many orders of magnitude larger than that ofd1.

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 matrixAand view the pattern of nonzero elements.

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); spy(A)

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

Calculate the determinant ofA.

d = det(A)
d = 1.1127e+05

The determinant ofAis quite large despite the fact thatAis singular. In fact, the determinant ofA应该完全零!The inaccuracy ofdis due to an aggregation of round-off errors in the MATLAB® implementation of the LU decomposition, whichdetuses to calculate the determinant. This result demonstrates a few important aspects of calculating numeric determinants. See theLimitationssection for more details.

Input Arguments

collapse all

Input matrix, specified as a square numeric matrix.

Data Types:single|double
Complex Number Support:Yes

Limitations

Avoid usingdetto examine if a matrix is singular because of the following limitations. Usecondorrcondinstead.

Limitation Result

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.

detuses 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,detcan produce a large-magnitude determinant for a singular matrix, even though it should have a magnitude of 0.

Algorithms

detcomputes the determinant from the triangular factors obtained by Gaussian elimination with thelufunction.

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

Extended Capabilities

Introduced before R2006a