%% LU Factorization of Matrix
% Compute the LU factorization of a matrix and examine the resulting factors. LU factorization is a way of decomposing a matrix A into an upper triangular matrix U, a lower triangular matrix L, and a permutation matrix P such that \textrm{PA}=\textrm{LU}. These matrices describe the steps needed to perform Gaussian elimination on the matrix until it is in reduced row echelon form. The L matrix contains all of the multipliers, and the permutation matrix P accounts for row interchanges.
%
% Create a 3-by-3 matrix and calculate the LU factors.

A = [10 -7 0
     -3  2 6
      5 -1 5];

%

[L,U] = lu(A)

%
% Multiply the factors to recreate A. With the two-input syntax, lu incorporates the permutation matrix P directly into the L factor, such that the L being returned is really P'*L and thus A = L*U.

L*U

%
% You can specify three outputs to separate the permutation matrix from the multipliers in L.

[L,U,P] = lu(A)

%

P'*L*U

% Copyright 2018 The MathWorks, Inc.