1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>LU LU Decomposition for Matrices
</TITLE>
</HEAD>
<BODY>
<H2>LU LU Decomposition for Matrices
</H2>
<P>
Section: <A HREF=sec_transforms.html> Transforms/Decompositions </A>
<H3>Usage</H3>
Computes the LU decomposition for a matrix. The form of the
command depends on the type of the argument. For full (non-sparse)
matrices, the primary form for <code>lu</code> is
<PRE>
[L,U,P] = lu(A),
</PRE>
<P>
where <code>L</code> is lower triangular, <code>U</code> is upper triangular, and
<code>P</code> is a permutation matrix such that <code>L*U = P*A</code>. The second form is
<PRE>
[V,U] = lu(A),
</PRE>
<P>
where <code>V</code> is <code>P'*L</code> (a row-permuted lower triangular matrix),
and <code>U</code> is upper triangular. For sparse, square matrices,
the LU decomposition has the following form:
<PRE>
[L,U,P,Q,R] = lu(A),
</PRE>
<P>
where <code>A</code> is a sparse matrix of either <code>double</code> or <code>dcomplex</code> type.
The matrices are such that <code>L*U=P*R*A*Q</code>, where <code>L</code> is a lower triangular
matrix, <code>U</code> is upper triangular, <code>P</code> and <code>Q</code> are permutation vectors
and <code>R</code> is a diagonal matrix of row scaling factors. The decomposition
is computed using UMFPACK for sparse matrices, and LAPACK for dense
matrices.
<H3>Example</H3>
First, we compute the LU decomposition of a dense matrix.
<PRE>
--> a = float([1,2,3;4,5,8;10,12,3])
a =
1 2 3
4 5 8
10 12 3
--> [l,u,p] = lu(a)
l =
1.0000 0 0
0.1000 1.0000 0
0.4000 0.2500 1.0000
u =
10.0000 12.0000 3.0000
0 0.8000 2.7000
0 0 6.1250
p =
0 0 1
1 0 0
0 1 0
--> l*u
ans =
10 12 3
1 2 3
4 5 8
--> p*a
ans =
10 12 3
1 2 3
4 5 8
</PRE>
<P>
Now we repeat the exercise with a sparse matrix, and demonstrate
the use of the permutation vectors.
<PRE>
--> a = sparse([1,0,0,4;3,2,0,0;0,0,0,1;4,3,2,4])
a =
1 1 1
2 1 3
4 1 4
2 2 2
4 2 3
4 3 2
1 4 4
3 4 1
4 4 4
--> [l,u,p,q,r] = lu(a)
l =
1 1 1
2 2 1
3 3 1
4 4 1
u =
1 1 0.153846
1 2 0.230769
2 2 0.4
1 3 0.307692
2 3 0.6
3 3 0.2
1 4 0.307692
3 4 0.8
4 4 1
p =
4
2
1
3
q =
3
2
1
4
r =
1 1 0.2
2 2 0.2
3 3 1
4 4 0.0769231
--> full(l*a)
ans =
1 0 0 4
3 2 0 0
0 0 0 1
4 3 2 4
--> b = r*a
b =
1 1 0.2
2 1 0.6
3 1 0
4 1 0.307692
1 2 0
2 2 0.4
3 2 0
4 2 0.230769
1 3 0
2 3 0
3 3 0
4 3 0.153846
1 4 0.8
2 4 0
3 4 1
4 4 0.307692
--> full(b(p,q))
ans =
0.1538 0.2308 0.3077 0.3077
0 0.4000 0.6000 0
0 0 0.2000 0.8000
0 0 0 1.0000
</PRE>
<P>
</BODY>
</HTML>
|