what is the difference between inv() and \ (the backslash) ?

254 views (last 30 days)
what is the difference between those functions and which one is more precise?
sometimes matlab show warning while using the inv function with a suggestion to use the backslash insted.
  1 Comment
Mark Spong
Mark Spong on 8 Jun 2022
I've been following the advice to use mldivide instead of inv; however, just now I am doing a Newton-Raphson optimization problem and F\x does not work, whereas inv(F)*x works perfectly. The backslash warns me that the matrix is singular to working precision and the algorithm fails to converge. The inv() function gives no such warning and converges rapidly. It is particularly confusing since F\x should compute a pseudo-inverse when the matrix is not invertible. Has anyone had a similar problem?

Sign in to comment.

Answers (2)

Roger Stafford
Roger Stafford on 1 Jul 2014
If A is an n x n matrix and B is n x m, solving A\B is tantamount to solving m*n equations in m*n unknowns. Finding the inverse of A is equivalent to finding A\eye(n), and hence is similar to solving n*n equations in n*n unknowns. If the number of columns, m, in B is less than n, it therefore takes less time to solve m*n equations than doing inv(A)*B which would involve n*n equations combined with a matrix multiplication.
If A is n x p and not square with p < n, solving A\B requires solving m*n equations with only m*p unknowns and is overdetermined, so A\B will simply find the best least squares approximation to a solution, which makes it different from 'inv' which will produce an error.
On the other hand if p > n the number of unknowns exceeds the number of equations and the system is underdetermined. Hence A\B will assign some of the unknowns arbitrary values. In this it also differs from the 'inv' function which will again give an error.

James Tursa
James Tursa on 1 Jul 2014
Edited: James Tursa on 1 Jul 2014
In general, use backslash \ whenever possible. It will be more accurate. The inv() function has its uses when you need the explicit inverse for some reason and you know the system is well behaved, but inv() should not be your first choice if backslash is an option. See the comments in "doc inv":

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!