Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function named ubitname_get_LU () that takes (as input) any size square

ID: 3572302 • 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 show 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 square matrices, so if the input matrix is rectangular, your function must throw an error (see Deliverable #4). Furthermore, Crout's method (and yours) unfortunately cannot handle all singular matrices. Therefore, after testing for "rectangularity, " your function 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.'

Explanation / Answer

script.m

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
endfunction

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote