MATLAB Answers


solving linear equations in a loop

Asked by Krzysztof
on 21 Mar 2014
Latest activity Edited by Matt J
on 21 Mar 2014
Accepted Answer by Matt J

The documentation for inv says:

A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve this is with x = inv(A)*b. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator x = A\b.

The above taken for granted, is it nevertheless reasonable to precalculate invA = inv(A) in front of a loop containing (A\Xi), especially one that necessarily sequential?



1 Answer

Answer by Matt J
on 21 Mar 2014
Edited by Matt J
on 21 Mar 2014
 Accepted answer

No. Make the different Xi the columns of matrix, X and just do A\X.


In case you did not have time to have a look at the algorithm: X(i+1) depends on A\X(i).

Matt J
on 21 Mar 2014

Probably better, then to pre-compute the LU decomposition


and then solve the system for each Xi using (U\(L\Xi)). You might improve things still further with an L,U,P decomposition.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!