|MATLAB Function Reference|
Y = inv(X)
returns the inverse of the square matrix
X. A warning message is printed if
X is badly scaled or nearly singular.
In practice, 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 . One way to solve this is with
inv(A)*b. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator
A\b. This produces the solution using Gaussian elimination, without forming the inverse. See
/ for further information.
Here is an example demonstrating the difference between solving a linear system by inverting the matrix with
inv(A)*b and solving it directly with
A\b. A random matrix
A of order 500 is constructed so that its condition number,
1.e10, and its norm,
1. The exact solution
x is a random vector of length 500 and the right-hand side is
A*x. Thus the system of linear equations is badly conditioned, but consistent.
On a 300 MHz, laptop computer the statements
while the statements
It takes almost two and one half times as long to compute the solution with
= inv(A)*b as with
A\b. Both produce computed solutions with about the same error,
1.e-6, reflecting the condition number of the matrix. But the size of the residuals, obtained by plugging the computed solution back into the original equations, differs by several orders of magnitude. The direct solution produces residuals on the order of the machine accuracy, even though the system is badly conditioned.
The behavior of this example is typical. Using
A\b instead of
inv(A)*b is two to three times as fast and produces residuals on the order of machine accuracy, relative to the magnitude of the data.
inv uses LAPACK routines to compute the matrix inverse:
The arithmetic operators
 Anderson, E., Z. Bai, C. Bischof, S. Blackford, J. Demmel, J. Dongarra, J. Du Croz, A. Greenbaum, S. Hammarling, A. McKenney, and D. Sorensen, LAPACK User's Guide (http://www.netlib.org/lapack/lug/ lapack_lug.html), Third Edition, SIAM, Philadelphia, 1999.