Please code the following in MATLAB. Thank you so much!! 4 LU Decomposition (4 p
ID: 3605958 • Letter: P
Question
Please code the following in MATLAB. Thank you so much!!
4 LU Decomposition (4 points) In this part, you will write a function that performs LU decomposition without pivotig. We will deal with pivoting in the next part of the assignment Specification: function [L, U] my-lu(A) Input: an n x n square matrix A Output: ·L: an n × n lower triangular matrix where the diagonal entries are all one, e.g.,*1 0where is a potentially nonzero element . U: an n x n upper triangular matrix. To get full credit, your function should handle the following case: Early termination Due to round-off error, there may exist free variables whose pivots are extremely small but not precisely zero. You should terminate your LU decomposition if the absolute value of a pivot is less than 10-12 The process of LU decomposition uses Gaussian elimination that transforms A to an upper triangular matrix U while recording the pivot multipliers in a lower triangular matrix L 1. Initialize L to the identity matrix, and U to A. You can use MATLAB's built-in function eye (n) 2. At the ith step, for each row k belowteh row, (a) Record the pivot multiplier to L at (k,i), ie., Lk.i = Uk,i/Ui,i as shown in Figure 2. Note that this fills in the ith column of L (b) Reduce the kth row of U using the pivot multiplier, i.e., Uk Uk(Un/U,JU where U, is the ith row 1 0 0-0 0 k-10 0 0 * * Figure 2: LU decomposition at the ith step.Explanation / Answer
function [L,U] = mylu(A) % syntax: [L,U] = mylu(A) % input: matrix A % perform an LU factorization of A [m,n] = size(A); L = zeros(n,n); U = zeros(n,n); for i=1:n L(i:n,i) = A(i:n,i); if i > 1 J = 1:i-1; L(i:n,i) = L(i:n,i) - L(i:n,J)*U(J,i); U(i,i:n) = [1.0 (A(i,i+1:n)-L(i,J)*U(J,i+1:n))/L(i,i)]; else U(i,i:n) = [1.0 A(i,i+1:n)/L(i,i)]; end; end; return;