Based on the wonderful MATLAB documentation, it is clear that when solving systems of linear equations, it is preferable to use X\b
to inv(X)*b
for reasons of numerical stability. It also notes that X^(-1)
is equivalent to inv(X)
. But it makes no mention of the relationship between X\eye(size(X))
and inv(X)
! I would like to use the more succinct notation, and it makes sense that inv(X)
should be the preferred form, but cannot find confirmation of this anywhere in the documentation.
The docs mention that \
uses Gaussian elimination whereas inv
uses LU decomposition.
When I tested them using an example similar to that provided in the documenation, I find that the two are not equivalent:
n = 500;
Q = orth(randn(n,n));
d = logspace(0,-10,n);
A = Q*diag(d)*Q';
y = inv(A);
z = A\eye(n);
disp(['Difference: ', num2str(norm(inv(A) - A\eye(n)))]);
The results:
Difference: 2.2957e-05
Can anyone explain which of the two methods is more numerically stable?