# lu

LU factorization

## Syntax

``[L,U] = lu(A)``
``[L,U,P] = lu(A)``
``[L,U,p] = lu(A,'vector')``
``[L,U,p,q] = lu(A,'vector')``
``[L,U,P,Q,R] = lu(A)``
``[L,U,p,q,R] = lu(A,'vector')``
``lu(A)``

## Description

example

````[L,U] = lu(A)` returns an upper triangular matrix `U` and a matrix `L`, such that ```A = L*U```. Here, `L` is a product of the inverse of the permutation matrix and a lower triangular matrix.```

example

````[L,U,P] = lu(A)` returns an upper triangular matrix `U`, a lower triangular matrix `L`, and a permutation matrix `P`, such that `P*A = L*U`. The syntax `lu(A,'matrix')` is identical.```

example

````[L,U,p] = lu(A,'vector')` returns the permutation information as a vector `p`, such that `A(p,:) = L*U`.```

example

````[L,U,p,q] = lu(A,'vector')` returns the permutation information as two row vectors `p` and `q`, such that `A(p,q) = L*U`.```

example

````[L,U,P,Q,R] = lu(A)` returns an upper triangular matrix `U`, a lower triangular matrix `L`, permutation matrices `P` and `Q`, and a scaling matrix `R`, such that `P*(R\A)*Q = L*U`. The syntax `lu(A,'matrix')` is identical.```

example

````[L,U,p,q,R] = lu(A,'vector')` returns the permutation information in two row vectors `p` and `q`, such that `R(:,p)\A(:,q) = L*U`.```

example

````lu(A)` returns the matrix that contains the strictly lower triangular matrix `L` (the matrix without its unit diagonal) and the upper triangular matrix `U` as submatrices. Thus, `lu(A)` returns the matrix `U + L - eye(size(A))`, where `L` and `U` are defined as ```[L,U,P] = lu(A)```. The matrix `A` must be square.```

## Examples

collapse all

Compute the LU factorization of this matrix. Because the numbers are not symbolic objects, you get floating-point results.

```M = [2 -3 -1; 1/2 1 -1; 0 1 -1]; [L, U] = lu(M)```
```L = 1.0000 0 0 0.2500 1.0000 0 0 0.5714 1.0000 U = 2.0000 -3.0000 -1.0000 0 1.7500 -0.7500 0 0 -0.5714```

Now convert this matrix to a symbolic object, and compute the LU factorization.

```M = sym(M); [L, U] = lu(M)```
```L = [ 1, 0, 0] [ 1/4, 1, 0] [ 0, 4/7, 1] U = [ 2, -3, -1] [ 0, 7/4, -3/4] [ 0, 0, -4/7]```

Return the lower and upper triangular matrices and the permutation matrix by providing three output arguments.

```syms a [L, U, P] = lu(sym([0 0 a; a 2 3; 0 a 2]))```
```L = [ 1, 0, 0] [ 0, 1, 0] [ 0, 0, 1] U = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a] P = 0 1 0 0 0 1 1 0 0```

Return the permutation information as a vector by using the `'vector'` flag.

```syms a A = [0 0 a; a 2 3; 0 a 2]; [L, U, p] = lu(A, 'vector')```
```L = [ 1, 0, 0] [ 0, 1, 0] [ 0, 0, 1] U = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a] p = 2 3 1```

Check that `A(p,:) = L*U` by using `isAlways`.

`isAlways(A(p,:) == L*U)`
```ans = 3×3 logical array 1 1 1 1 1 1 1 1 1```

Restore the permutation matrix `P` from the vector `p`.

```P = zeros(3, 3); for i = 1:3 P(i, p(i)) = 1; end P```
```P = 0 1 0 0 0 1 1 0 0```

Return the permutation information as two vectors `p` and `q`.

```syms a A = [a, 2, 3*a; 2*a, 3, 4*a; 4*a, 5, 6*a]; [L, U, p, q] = lu(A, 'vector')```
```L = [ 1, 0, 0] [ 2, 1, 0] [ 4, 3, 1] U = [ a, 2, 3*a] [ 0, -1, -2*a] [ 0, 0, 0] p = 1 2 3 q = 1 2 3```

Check that `A(p, q) = L*U` by using `isAlways`.

`isAlways(A(p, q) == L*U)`
```ans = 3×3 logical array 1 1 1 1 1 1 1 1 1```

Return the lower and upper triangular matrices, permutation matrices, and scaling matrix.

```syms a A = [0, a; 1/a, 0; 0, 1/5; 0,-1]; [L, U, P, Q, R] = lu(A)```
```L = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 1/(5*a), 1, 0] [ 0, -1/a, 0, 1] U = [ 1/a, 0] [ 0, a] [ 0, 0] [ 0, 0] P = 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 Q = 1 0 0 1 R = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 0, 1, 0] [ 0, 0, 0, 1]```

Check that `P*(R\A)*Q = L*U` by using `isAlways`.

`isAlways(P*(R\A)*Q == L*U)`
```ans = 4×2 logical array 1 1 1 1 1 1 1 1```

Return the permutation information as vectors `p` and `q` by using the `'vector'` flag. Also, compute the scaling matrix `R`.

```syms a A = [0, a; 1/a, 0; 0, 1/5; 0,-1]; [L, U, p, q, R] = lu(A,'vector')```
```L = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 1/(5*a), 1, 0] [ 0, -1/a, 0, 1] U = [ 1/a, 0] [ 0, a] [ 0, 0] [ 0, 0] p = 2 1 3 4 q = 1 2 R = [ 1, 0, 0, 0] [ 0, 1, 0, 0] [ 0, 0, 1, 0] [ 0, 0, 0, 1]```

Check that `R(:,p)\A(:,q) = L*U` by using `isAlways`.

`isAlways(R(:,p)\A(:,q) == L*U)`
```ans = 4×2 logical array 1 1 1 1 1 1 1 1```

Return triangular matrices as submatrices by specifying one or no output arguments.

```syms a A = [0 0 a; a 2 3; 0 a 2]; lu(A)```
```ans = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a]```

Verify that the resulting matrix is equal to ```U + L - eye(size(A))```, where `L` and `U` are defined as `[L,U,P] = lu(A)`.

```[L,U,P] = lu(A); U + L - eye(size(A))```
```ans = [ a, 2, 3] [ 0, a, 2] [ 0, 0, a]```

## Input Arguments

collapse all

Input, specified as a numeric or symbolic matrix.

collapse all

### LU Factorization of a Matrix

LU factorization expresses an m-by-n matrix A as P* A = L *U. Here, L is an m-by-m lower triangular matrix, U is an m-by-n upper triangular matrix, and P is a permutation matrix.

### Permutation Vector

The permutation vector `p` contains numbers corresponding to row exchanges in the matrix `A`. For an m-by-m matrix, `p` represents the following permutation matrix with indices i and j ranging from 1 to m.

## Tips

• Calling `lu` for numeric arguments that are not symbolic objects invokes the MATLAB® `lu` function.

• The `thresh` option supported by the MATLAB `lu` function does not affect symbolic inputs.

• If you use `'matrix'` instead of `'vector'`, then `lu` returns permutation matrices, as it does by default.

• `L` and `U` are nonsingular if and only if `A` is nonsingular. `lu` also can compute the LU factorization of a singular matrix `A`. In this case, `L` or `U` is a singular matrix.

• Most algorithms for computing LU factorization are variants of Gaussian elimination.