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

205 visualizaciones (últimos 30 días)
Kobi
Kobi el 1 de Jul. de 2014
Comentada: Mark Spong el 8 de Jun. de 2022
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 comentario
Mark Spong
Mark Spong el 8 de Jun. de 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?

Iniciar sesión para comentar.

Respuestas (2)

Roger Stafford
Roger Stafford el 1 de Jul. de 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 el 1 de Jul. de 2014
Editada: James Tursa el 1 de Jul. de 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":

Categorías

Más información sobre Linear Algebra en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by