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

243 次查看(过去 30 天)
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 个评论
Mark Spong
Mark Spong 2022-6-8
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?

请先登录,再进行评论。

回答(2 个)

Roger Stafford
Roger Stafford 2014-7-1
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 2014-7-1
编辑:James Tursa 2014-7-1
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":

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by