Write a function named ubi Zr.axe qet LU () that takes (as input) any size squar
ID: 3573746 • Letter: W
Question
Write a function named ubi Zr.axe qet 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 Grout's method (and also your method, which is derived from Grout'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, Grout'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 dot () function. If the input matrix is singular, your function must throw an error using the built-in MATIAB function error {} with the message 'Matrix is singular; the LU decomposition cannot be computed by this function. (Note: MATLAB's lu() function is able to handle rectangular and singular matrices.)Explanation / Answer
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
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
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.