Only if you explicitly need the inverse of a matrix you use inv()
, otherwise you just use the backslash operator
.
The documentation on inv()
explicitly states:
x = A
is computed differently than x = inv(A)*b
and is recommended for solving systems of linear equations.
This is because the backslash operator, or mldivide()
uses whatever method is most suited for your specific matrix:
x = AB
solves the system of linear equations A*x = B
. The matrices A
and B
must have the same number of rows. MATLAB? displays a warning message if A
is badly scaled or nearly singular, but performs the calculation regardless.
Just so you know what algorithm MATLAB chooses depending on your input matrices, here's the full algorithm flowchart as provided in their documentation
The versatility of mldivide
in solving linear systems stems from its ability to take advantage of symmetries in the problem by dispatching to an appropriate solver. This approach aims to minimize computation time. The first distinction the function makes is between full (also called "dense") and sparse input arrays.
As a side-note about error of order of magnitude 10^(-12)
, besides the above mentioned inaccuracy of the inv()
function, there's floating point accuracy. This post on MATLAB issues on it is rather insightful, with a more general computer science post on it here. Basically, if you are computing numerics, don't worry (too much at least) about errors 12 orders of magnitude smaller.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…