Documentation

inv

Matrix inverse

Syntax

Description

example

Y = inv(X)computes theinverseof square matrixX.

  • X^(-1)is equivalent toinv(X).

  • x = A\bis computed differently thanx = inv(A)*band is recommended for solving systems of linear equations.

Examples

collapse all

Compute the inverse of a 3-by-3 matrix.

X = [1 0 2; -1 5 0; 0 3 -9]
X =3×31 0 2 -1 5 0 0 3 -9
Y = inv(X)
Y =3×30.8824 -0.1176 0.1961 0.1765 0.1765 0.0392 0.0588 0.0588 -0.0980

Check the results. Ideally,Y*Xproduces the identity matrix. Sinceinvperforms the matrix inversion using floating-point computations, in practiceY*Xis close to, but not exactly equal to, the identity matrixeye(size(X)).

Y*X
ans =3×31.0000 0.0000 -0.0000 0 1.0000 -0.0000 0 -0.0000 1.0000

Examine why solving a linear system by inverting the matrix usinginv(A)*bis inferior to solving it directly using the backslash operator,x = A\b.

创建一个随机矩阵Aof order 500 that is constructed so that its condition number,cond(A), is1e10, and its norm,norm(A), is1. The exact solutionxis a random vector of length 500, and the right side isb = A*x. Thus the system of linear equations is badly conditioned, but consistent.

n = 500; Q = orth(randn(n,n)); d = logspace(0,-10,n); A = Q*diag(d)*Q'; x = randn(n,1); b = A*x;

Solve the linear systemA*x = bby inverting the coefficient matrixA. Useticandtocto get timing information.

tic y = inv(A)*b; t = toc
t = 0.0302

Find the absolute and residual error of the calculation.

err_inv = norm(y-x)
err_inv = 4.3020e-06
res_inv = norm(A*y-b)
res_inv = 4.1853e-07

Now, solve the same linear system using the backslash operator\.

tic z = A\b; t1 = toc
t1 = 0.0126
err_bs = norm(z-x)
err_bs = 3.1579e-06
res_bs = norm(A*z-b)
res_bs = 2.7938e-15

The backslash calculation is quicker and has less residual error by several orders of magnitude. The fact thaterr_invanderr_bsare both on the order of1e-6simply reflects the condition number of the matrix.

The behavior of this example is typical. UsingA\binstead ofinv(A)*bis two to three times faster, and produces residuals on the order of machine accuracy relative to the magnitude of the data.

Input Arguments

collapse all

Input matrix, specified as a square matrix. IfXis badly scaled or nearly singular, then theinvcalculation loses numerical accuracy. Usercondorcondto check the condition number of the matrix.

Data Types:single|double
Complex Number Support:Yes

更多的About

collapse all

Matrix Inverse

一个matrixXis invertible if there exists a matrixYof the same size such that X Y = Y X = I n , where I n is then-by-nidentity matrix. The matrixYis called the inverse ofX.

一个matrix that has no inverse is singular. A square matrix is singular only when its determinant is exactly zero.

Tips

  • It is seldom necessary to form the explicit inverse of a matrix. A frequent misuse ofinvarises when solving the system of linear equationsAx=b. One way to solve the equation is withx = inv(A)*b. A better way, from the standpoint of both execution time and numerical accuracy, is to use the matrix backslash operatorx = A\b. This produces the solution using Gaussian elimination, without explicitly forming the inverse. Seemldividefor further information.

Algorithms

invperforms an LU decomposition of the input matrix (or an LDL decomposition if the input matrix is Hermitian). It then uses the results to form a linear system whose solution is the matrix inverseinv(X). For sparse inputs,inv(X)creates a sparse identity matrix and uses backslash,X\speye(size(X)).

Extended Capabilities

See Also

|||

Introduced before R2006a