Write a function named ubitname_get_LU() that takes (as input) any size square m
ID: 3573178 • Letter: W
Question
Write a function named ubitname_get_LU() that takes (as input) any size square matrix and returns (as outputs) the matrices L and U from its LU decomposition in the conventional format of Eq. (1), which is consistent with the method discussed in class. To do this, you must follow the same methodology used for Crout's method shown on pages 5-10 by first re-deriving the general equations that are equivalent to equations (4.34-38) of Ref (1), but using the conventional format for L and U shown in Eq (1). You must clearly shown your work in your paper copy of the project including the re-derivation of the general-equations-starting from eq. (1)-that will be used later in your function to obtain the L and U matrices. However, using Crout's method (and also your method, which is derived from Crout's method). The LU decomposition can only be obtained for squarer matrices, so if the input matrix is rectangular, your functions must test if the input matrix is singular using MATLAB's built-in det() function. If the input matrix is singular, your function must throw an error using the built-in MATLAB function error() with the message 'Matrix is singular: the LU decomposition cannot be computed by this function." The new code should incorporate that L should have a diagonal of ones shown below This is the original code that needs to be adjusted with the new information. Function (L, U) = LUdecompCrout(A) % The function decomposes the matrix A into a lower triangular matrix L % and an upper triangular matrix U, using Crout's method such that A=LU. % Input variable: % The matrix of coefficients. % Output variables: % L Lower triangular matrix. % Upper triangular matrix. [R, C] = size(A); for i =1:R L(i, 1) = A(i, 1); U(i, i) = 1; end for j = 2:R U(1, j) = A(1, j)/L(1, 1); end for i = 2:R for j = 2: i L(i, j) = A(i, j) - L(i, 1:(j - 1)) * U(1:(j - 1), j); end for j = (i + 1):R U(i, j) = (A(i, j) - L(i, 1:(i - 1)) * U(1:(i - 1), j))/L(i, i); end end % for i = 2:R end % function [L, U] = LUdecompCrout(A)Explanation / Answer
script.m
A = [1 2 5;
2 5 4;
4 7 5];
if size(A, 1) != size(A, 2)
error('The matrix is rectangular');
end
[L, U] = ubitname_get_LU(A);
disp(L);
disp(U);
tol = 0.00001;
if det(A)<tol
error('The matrix is singular.');
end
ubitname_get_LU.m
function [L,U]=ubitname_get_LU(A)
[r,c]=size(A);
for i=1:r
L(i,1)=A(i,1);
U(i,i)=1;
end
for j=2:r
U(1,j)=A(1,j)/L(1,1);
end
for i=2:r
for j=2:i
L(i,j)=A(i,j)-L(i,1:j-1)*U(1:j -1,j);
end
for j=i+1:r
U(i,j)=(A(i,j)-L(i,1:i-1)*U(1:i-1,j))/L(i,i);
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.