In: Advanced Math
Q: (LU decomposition) Find the LU decomposition of A = [-3 2 5 1; 12 -4 -20 -2; -6 0 15 1; -9 6 35 4]. You can use the compact method which works within a single matrix or you can build L and U separately. State L and U explicitly, and verify (in Matlab) that A = L*U. Hint: Matlab's built-in lu function isn't useful, since it pivots.
%Matlab code for solving linear equation without pivoting
clear all
close all
%The A matrix and corresponding solution matrix
A=[-3 2 5 1; 12 -4 -20 -2; -6 0 15 1; -9 6 35 4];
%Displaying the matrix
fprintf(' The A matrix is\n')
disp(vpa(A,3))
%LU factorization using function lu_nopivot
[L, U] = lu_nopivot(A);
fprintf('The L matrix is\n')
disp(vpa(L,3))
fprintf('The U matrix is\n')
disp(vpa(U,3))
fprintf('L*U=\n')
disp(vpa(L*U,3))
%Matlab function for lu factorization
function [L, U] = lu_nopivot(A)
n = size(A, 1); % Obtain number of rows
(should equal number of columns)
L = eye(n); % Start L off as identity and
populate the lower triangular half slowly
for k = 1 : n
% For each row k, access
columns from k+1 to the end and divide by
% the diagonal
coefficient at A(k ,k)
L(k + 1 : n, k) = A(k +
1 : n, k) / A(k, k);
% For each row k+1 to
the end, perform Gaussian elimination
% In the end, A will
contain U
for l = k + 1 : n
A(l, :) = A(l, :) - L(l, k) * A(k, :);
end
end
U = A;
end
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%