AAE-NA-Labs/Direct Methods for Solving Linear Systems/Alg7.m

35 lines
720 B
Mathematica
Raw Normal View History

function [L, U] = Alg7(A)
2021-03-13 20:23:23 +01:00
% LU Factorization without pivoting
% [L, U] = Alg7(A) decomposes matrix A using Crout's algorithm into
% U upper triangular matrix and L unit lower triangular matrix
% such that A = LU.
[m, n] = size(A);
2021-03-13 19:02:40 +01:00
% This solution is mathematically correct, however computationaly
% inefficient.
%
% A = Alg1_outer_product_gaussian_elimination(A);
%
% U = triu(A);
% L = tril(A, -1) + eye(m);
2021-03-13 19:02:40 +01:00
% Instead, we should use the Crout's algorithm
2021-03-13 20:23:23 +01:00
L = zeros(m, n);
U = eye(m, n);
2021-03-13 19:02:40 +01:00
for k = 1:n
for j = k : n
2021-03-13 20:23:23 +01:00
U(k, j) = A(k, j) - dot(L(k, 1:k-1), U(1:k-1, j));
2021-03-13 19:02:40 +01:00
end
2021-03-13 20:23:23 +01:00
for i = k:n
L(i, k) = (A(i, k) - dot(L(i,1:k-1), U(1:k-1, k))) / U(k,k);
end
2021-03-13 19:02:40 +01:00
end
end