필터 지우기
필터 지우기

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

조회 수: 205 (최근 30일)
Kobi
Kobi 2014년 7월 1일
댓글: Mark Spong 2022년 6월 8일
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에서 Linear Algebra에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by