-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDU.m
62 lines (61 loc) · 1.5 KB
/
LDU.m
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
% input: an orthogonal matrix A (NxN)
% output: matrixes L, D, U and P (all must be of size NxN)
function [L,D,U,P] = LDU(A)
[rA, cA] = size(A);
if rA ~= cA
output = "Bad Input!"
L = [];
D = [];
U = [];
P = [];
else
L = zeros(rA, cA);
D = zeros(rA, cA);
U = zeros(rA, cA);
P = zeros(rA, cA);
for i = 1 : rA
for j = 1 : cA
if (i == j)
P(i,j) = 1;
end
end
end
U = A;
for i = 1 : (cA - 1)
for j = i + 1 : cA
if (U(i,i) ~= 0)
l = U(j,i) / U(i,i);
L(j,i) = l;
U(j,:) = U(j,:) - l * U(i,:);
else
temp = U(i,:);
U(i,:) = U(i + 1, :);
U(i + 1,:) = temp;
temp = P(i,:);
P(i,:) = P(i + 1, :);
P(i + 1,:) = temp;
temp = L(i,:);
L(i,:) = L(i + 1, :);
L(i + 1,:) = temp;
end
end
end
for i = 1 : rA
for j = 1 : cA
if (i == j)
L(i,j) = 1;
end
end
end
for i = 1 : rA
if (U(i,i) ~= 0)
D(i,i) = U(i,i);
U(i, :) = U(i, :) / U(i,i);
end
end
end
L
D
U
P
end