-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlu_decomp.m
More file actions
38 lines (32 loc) · 966 Bytes
/
lu_decomp.m
File metadata and controls
38 lines (32 loc) · 966 Bytes
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
% This function performs the LU-Decomposition algorithm
% of a given matrix A. A is assumed to be non-singular.
%
% Returns a unit-lower triangular matrix L
% and an upper triangular matrix U.
function [L, U] = lu_decomp(A)
% Calculating the dimensions of A
% and A is a square matrix.
n = size(A, 1);
% Initializing L and U.
L = zeros(n) + eye(n);
U = zeros(n);
% Beginning the LU-Decomposition Algorithm.
for r = 1:n
% Recovering the rows of U.
for i = r:n
temp = 0;
for j = 1:(r-1)
temp = temp + L(r, j) * U(j, i);
end
U(r, i) = A(r, i) - temp;
end
% Recovering the columns of L.
for i = (r+1):n
temp = 0;
for j = 1:(r-1)
temp = temp + L(i, j) * U(j, r);
end
L(i, r) = (A(i, r) - temp) / U(r, r);
end
end
end