When solving for x in Ax=b, why is A\b faster that A^-1 * b?

9 views (last 30 days)
if true
% code
A = [1,2,-1;2,5,-1;1,3,-3];
b = [6;13;4];
tic
for i=1:1000
X = A\b;
end
toc
tic
for i=1:1000
x = A^-1 * b;
end
toc
Elapsed time is 0.003520 seconds. Elapsed time is 0.017500 seconds.

Accepted Answer

Stephen23
Stephen23 on 11 Mar 2018
Edited: Stephen23 on 11 Mar 2018
Because \ is just one efficient command, rather than the two slow commands required with inv.
The inv documentation states "It is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve the equation is with x = inv(A)*b. A better way, from the standpoint of both execution time and numerical accuracy, is to use the matrix backslash operator x = A\b. This produces the solution using Gaussian elimination, without explicitly forming the inverse. See mldivide for further information."
So the documenation makes it clear that using mldivide is faster and produces better results. See also:

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!