[L,U,P,Q,D] = lu(S)
also returns a diagonal scaling matrix D such that
P*(D\S)*Q = L*U. Typically, the row-scaling leads to a
sparser and more stable factorization.

[___] = lu(S,thresh)
specifies thresholds for the pivoting strategy employed by
lu using any of the previous output argument
combinations. Depending on the number of output arguments specified, the default
value and requirements for the thresh input are different.
See the thresh argument description for details.

[___] = lu(___,outputForm)
returns P and Q in the form specified by
outputForm. Specify outputForm as
'vector' to return P and
Q as permutation vectors. You can use any of the input
argument combinations in previous syntaxes.

Examples

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 PA=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)

L = 3×3
1.0000 0 0
-0.3000 -0.0400 1.0000
0.5000 1.0000 0

U = 3×3
10.0000 -7.0000 0
0 2.5000 5.0000
0 0 6.2000

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.

Solve Linear System with LU Factorization

Solve a linear system by performing an LU factorization and using the factors to simplify the problem. Compare the results with other approaches using the backslash operator and decomposition object.

Create a 5-by-5 magic square matrix and solve the linear system Ax=b with all of the elements of b equal to 65, the magic sum. Since 65 is the magic sum for this matrix (all of the rows and columns add to 65), the expected solution for x is a vector of 1s.

A = magic(5);
b = 65*ones(5,1);
x = A\b

x = 5×1
1.0000
1.0000
1.0000
1.0000
1.0000

For generic square matrices, the backslash operator computes the solution of the linear system using LU decomposition. LU decomposition expresses A as the product of triangular matrices, and linear systems involving triangular matrices are easily solved using substitution formulas.

To recreate the answer computed by backslash, compute the LU decomposition of A. Then, use the factors to solve two triangular linear systems:

y = L\(P*b);
x = U\y;

This approach of precomputing the matrix factors prior to solving the linear system can improve performance when many linear systems will be solved, since the factorization occurs only once and does not need to be repeated.

The decomposition object also is useful to solve linear systems using specialized factorizations, since you get many of the performance benefits of precomputing the matrix factors but you do not need to know how to use the factors. Use the decomposition object with the 'lu' type to recreate the same results.

Save Memory with Permutation Vectors

Compute the LU factorization of a matrix. Save memory by returning the row permutations as a vector instead of a matrix.

Create a 1000-by-1000 random matrix.

A = rand(1000);

Compute the LU factorization with the permutation information stored as a matrix P. Compare the result with the permutation information stored as a vector p. The larger the matrix, the more memory efficient it is to use a permutation vector.

Using a permutation vector also saves on execution time in subsequent operations. For instance, you can use the previous LU factorizations to solve a linear system Ax=b. Although the solutions obtained from the permutation vector and permutation matrix are equivalent (up to roundoff), the solution using the permutation vector typically requires a little less time.

Now, calculate the LU factorization of A using lu with four outputs, which permutes the columns of A to reduce the number of nonzeros in the factors. The resulting factors are much sparser than if column permutations are not used.

S — Sparse input matrixsparse matrix

Pivoting thresholds for sparse matrices, specified as a scalar or
two-element vector. Valid values are in the interval [0
1]. The way you specify thresh depends on
how many outputs are specified in the call to lu:

For three outputs or less,
thresh must be a scalar, and the default
value is 1.0.

For four outputs or more,
thresh can be a scalar or a two element
vector. The default value is [0.1 0.001]. If
you specify thresh as a scalar, then that
only replaces the first value in the vector.

At a high level, this input enables you to make trade-offs between
accuracy and total execution time. Smaller values of
thresh tend to lead to sparser LU factors, but the
solution can become inaccurate. Larger values can lead to a more accurate
solution (but not always), and usually an increase in the total work and
memory usage.

lu selects a pivoting strategy based first on the
number of output arguments and second on the properties of the matrix being
factorized. In all cases, setting the threshold value(s) to
1.0 results in partial pivoting, while setting them
to 0 causes the pivots to be chosen only based on the
sparsity of the resulting matrix. All values of L have an
absolute value of 1/min(thresh) or less.

Three or fewer output arguments —
The algorithm selects the diagonal pivot if it satisfies the
equation

A(j,j) >= thresh * max(abs(A(j:m,j)))

Otherwise,
it selects the row that contains the element of largest absolute
value.

Symmetric Pivoting Strategy — If
S is a square sparse matrix with a mostly
symmetric structure and mostly nonzero diagonal, then
lu uses a symmetric pivoting strategy. For
this strategy, the algorithm selects the diagonal pivot
j if it satisfies the
inequality:

A(i,j) >= thresh(2) * max(abs(A(j:m,j)))

If
the diagonal entry fails this test, then lu
selects the sparsest row i satisfying the
inequality:

A(i,j) >= thresh(1) * max(abs(A(j:m,j)))

Nonsymmetric Pivoting Strategy —
If S does not satisfy the requirements for the
symmetric pivoting strategy, then lu uses a
nonsymmetric strategy. In this case, lu selects
the sparsest row i satisfying the
inequality:

A(i,j) >= thresh(1) * max(abs(A(j:m,j)))

A
value of 1.0 for thresh(1)
results in conventional partial pivoting. Entries in
L have an absolute value of
1/thresh(1) or less. The second element of
the thresh input vector is not used with the
nonsymmetric strategy.

Note

In some rare cases, an incorrect factorization results in
P*S*Q ≠ L*U. If this occurs,
increase thresh to a maximum of
1.0 (regular partial pivoting), and try
again.

Output Arguments

L — Lower triangular factormatrix

Lower triangular factor, returned as a matrix. The form of
L depends on whether the row permutations
P are returned in a separate output:

If the third output P is specified, then
L is returned as a unit lower triangular
matrix (that is, a lower triangular matrix with 1s on the main
diagonal).

If the third output P is not specified,
then L is returned as a row-permutation of a
unit lower triangular matrix. Specifically, it is the product
P'*L of the outputs P
and L returned in the three output
case.

U — Upper triangular factormatrix

Upper triangular factor, returned as an upper triangular matrix.

P — Row permutationvector | matrix

Row permutation, returned as a permutation matrix or, if the
'vector' option is specified, as a permutation
vector. Use this output to improve the numerical stability of the
calculation.

See outputForm for a description of the identities that this
output satisfies.

Q — Column permutationvector | matrix

Column permutation, returned as a permutation matrix or, if the
'vector' option is specified, as a permutation
vector. Use this output to reduce the fill-in (number of nonzeros) in the
factors of a sparse matrix.

See outputForm for a description of the identities that this
output satisfies.

D — Row scalingdiagonal matrix

Row scaling, returned as a diagonal matrix. D is used
to scale the values in S such that P*(D\S)*Q =
L*U. Typically, but not always, the row scaling leads to a
sparser and more stable factorization.

Algorithms

The LU factorization is computed using a variant of Gaussian elimination. Computing an
accurate solution is dependent upon the value of the condition number of the original
matrix cond(A). If the matrix has a large condition number (it is
nearly singular), then the computed factorization might not be accurate.

The LU factorization is a key step in obtaining the inverse with
inv and the determinant with det. It is also
the basis for the linear equation solution or matrix division obtained with the
operators \ and /. This necessarily means that the
numerical limitations of lu are also present in these dependent
functions.

Extended Capabilities

C/C++ Code GenerationGenerate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Code generation does not support sparse matrix inputs for this
function.

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic. By continuing to use this website, you consent to our use of cookies. Please see our Privacy Policy to learn more about cookies and how to change your settings.