inv - Matrix inverse - MATLAB (original) (raw)
Main Content
Syntax
Description
Y = inv([X](#bu6q31y-X))
computes the inverse of square matrix X
.
X^(-1)
is equivalent toinv(X)
.x = A\b
is computed differently thanx = inv(A)*b
and is recommended for solving systems of linear equations.
Examples
Compute the inverse of a 3-by-3 matrix.
X = [1 0 2; -1 5 0; 0 3 -9]
X = 3×3
1 0 2
-1 5 0
0 3 -9
Y = 3×3
0.8824 -0.1176 0.1961
0.1765 0.1765 0.0392
0.0588 0.0588 -0.0980
Check the results. Ideally, Y*X
produces the identity matrix. Since inv
performs the matrix inversion using floating-point computations, in practice Y*X
is close to, but not exactly equal to, the identity matrix eye(size(X))
.
ans = 3×3
1.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 using inv(A)*b
is inferior to solving it directly using the backslash operator, x = A\b
.
Create a random matrix A
of order 500 that is constructed so that its condition number, cond(A)
, is 1e10
, and its norm, norm(A)
, is 1
. The exact solution x
is a random vector of length 500, and the right side is b = 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 = Ax;
Solve the linear system A*x = b
by inverting the coefficient matrix A
. Use tic
and toc
to get timing information.
tic y = inv(A)*b; t = toc
Find the absolute and residual error of the calculation.
Now, solve the same linear system using the backslash operator \
.
The backslash calculation is quicker and has less residual error by several orders of magnitude. The fact that err_inv
and err_bs
are both on the order of 1e-6
simply reflects the condition number of the matrix.
The behavior of this example is typical. Using A\b
instead of inv(A)*b
is two to three times faster, and produces residuals on the order of machine accuracy relative to the magnitude of the data.
Input Arguments
Input matrix, specified as a square matrix. If X
is badly scaled or nearly singular, then the inv
calculation loses numerical accuracy. Use rcond or cond to check the condition number of the matrix.
Data Types: single
| double
Complex Number Support: Yes
More About
A matrix X
is invertible if there exists a matrix Y
of the same size such that XY=YX=In, where In is the n
-by-n
identity matrix. The matrix Y
is called the inverse of X
.
A 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 of
inv
arises when solving the system of linear equations Ax = 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. See mldivide for further information.
Algorithms
inv
performs 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 inverse inv(X)
. For sparse inputs, inv(X)
creates a sparse identity matrix and uses backslash, X\speye(size(X))
.
Extended Capabilities
Usage notes and limitations:
Singular matrix inputs can produce nonfinite values that differ from MATLAB® results.
For sparse matrix inputs, code generation supports only matrices of type
double
.For sparse matrix inputs, the language standard must be C99 or later.
X
must be nonsparse.The MATLAB
inv
function prints a warning ifX
is badly scaled or nearly singular. ThegpuArray
inv
is unable to check for this condition. Take action to avoid this condition.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Sparse matrices of type double
are supported for code generation.
The inv
function shows improved performance when operating on large triangular matrices.
For example, inverting a 5,000-by-5,000 upper triangular matrix is about 3.7x faster than in the previous release.
function timingInv rng default A = randn(5e3); [~,R] = lu(A);
tic Y = inv(R); toc end
The approximate execution times are:
R2021b: 1.1 s
R2022a: 0.3 s
The code was timed on a Windows® 10, Intel® Xeon® CPU W-2133 @ 3.60 GHz test system by calling thetimingInv
function.