MATLAB Answers

Kobi
2

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

Asked by Kobi
on 1 Jul 2014
Latest activity Answered by Roger Stafford on 1 Jul 2014
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.

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by 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.

  0 Comments

Sign in to comment.


Answer by James Tursa
on 1 Jul 2014
Edited by 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":

  0 Comments

Sign in to comment.