16

I read at a few places (in the doc and in this blog post : http://blogs.mathworks.com/loren/2007/05/16/purpose-of-inv/ ) that the use of inv in Matlab is not recommended because it is slow and inaccurate.

I am trying to find the reason of this inaccuracy. As of now, Google did not give m interesting result, so I thought someone here could guide me.

Thanks !

Amro
  • 123,847
  • 25
  • 243
  • 454
simark
  • 490
  • 3
  • 9

3 Answers3

33

The inaccuracy I mentioned is with the method INV, not MATLAB's implementation of it. You should be using QR, LU, or other methods to solve systems of equations since these methods don't typically require squaring the condition number of the system in question. Using inv typically requires an operation that loses accuracy by squaring the condition number of the original system.

--Loren

Loren
  • 1,725
  • 14
  • 6
19

I think the point of Loren's blog is not that MATLAB's inv function is particularly slower or more inaccurate than any other numerical implementation of computing a matrix inverse; rather, that in most cases the inverse itself is not needed, and you can proceed by other means (such as solving a linear system using \ - the backslash operator - rather than computing an inverse).

Edric
  • 23,676
  • 2
  • 38
  • 40
  • 1
    Moreover, the backslash operator (generally) gives more accurate results than inv(A)*b: it chooses a suitable algorithm to solve A*x=b. – Martijn Sep 14 '09 at 08:03
  • 2
    To be explicit for the students out there, you want to write `x = A \ b` instead of `x = inv(A) * b` in order to solve the linear system Ax = b. Computing the inverse of A is not necessary, not robust, and not fast. In a huge share of mathematical formulas where you see an A^-1, the algorithm can be implemented WITHOUT ever computing the inverse of A. That said, for small, full rank matrices, computing inv(A) will almost always be perfectly fine. For big matrices or ill conditioned matrices, it can get problematic. – Matthew Gunn Nov 10 '15 at 08:07
  • I don't understand why inv is not simply implemented as `inv(a) = a\I` where `I` is an identity matrix with the same size as `a`? I know this topic is old, but opening a new topic just for this question seems too much. – Paradoxy Oct 22 '21 at 21:08
1

inv() is certainly slower than \ unless you have multiple right hand side vectors to solve for. However, the advice from MathWorks regarding inaccuracy is due to a overly conservative bound in a numerical linear algebra result. In other words, inv() is NOT inaccurate. The link elaborates further : http://arxiv.org/abs/1201.6035

Several widely-used textbooks lead the reader to believe that solving a linear system of equations Ax = b by multiplying the vector b by a computed inverse inv(A) is inaccurate. Virtually all other textbooks on numerical analysis and numerical linear algebra advise against using computed inverses without stating whether this is accurate or not. In fact, under reasonable assumptions on how the inverse is computed, x = inv(A)*b is as accurate as the solution computed by the best backward-stable solvers.

Milind R
  • 676
  • 1
  • 8
  • 20