Please code the following in MATLAB. Thank you so much!! Use the code posted on
ID: 3606004 • Letter: P
Question
Please code the following in MATLAB. Thank you so much!!
Use the code posted on the bottom to solve problem 5:
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;
5 LU Decomposition with Partial Pivoting (4 points) Based on your my lu, you wl write numerically stable LU decomposition with partial pivoting. At the ith step of LU decompositionth pivot column), you will find the row that has the largest absolute value in the pivot column (say row j), and swap the ith and jth rows of U as usual. Simultaneously, you swap the partial entries of the ith and jth rows of L, and record the row exchange in a permutation matrix P. For further details, please see htt p: //ww . saath . kent·edu/-rai che1/courses/ intr , aun , conp. 1 /fal 109/ 1ecture9/1ecture4·pdf Specification function [L, U, P] = my-lup(A) Input: an n × n square matrix A Output: ·L: an n × n lower triangular matrix where the diagonal entries are all 1 ·U: an n × n upper triangular matrix. P: an n × permutation matrix The process of LU decomposition with partial pivoting needs to compute an additional row permutation matrix P 1. Initialize L and P to the identity matrix, and U to A. You can use MATLAB's built-in function eye(n) 2. At the ith step, (a) Similar to Assignment 1, perform partial pivoting in U (b) Record the row exchange to the permutation matrix P by exchanging its corresponding rows. (c) Exchange the corresponding row entries in L to reflect the row ex change in U. Note that this exchange only involves with the row entries that have been recorded.e ot entire row exchange (d) For each row k below the ith row, record the pivot multiplier to L and replace the row in U using the pivot multiplier, like in the previous part of this assignmentExplanation / Answer
Solution for Question:
This below Matlab code will solve the given Lux = b for a
given lower triangle matrix L, upper triangle matrix U and
right hand side vector b..
See the implementation of myLu function in matlab..
function [L,U]=mylu(A,nl,nu)
%Lu factorization with no pivoting
%input: A matrix
% nl number of lower diagonals
% nu number of upper diagonals
%output: L lower triangular matrix
% U upper triangular matrix
%
[n,m] = size(A);
U = zeros(n);
U(1,1:min(n,1+nu)) = A(1,1:min(1+nu,n));
d = zeros(1,n);
L = diag(d+1,0);
for i = 1:n-1
L(i+1:min(i+nl,n),i) = A(i+1:min(i+nl,n),i)/U(i,i);
for j = i+1:min(i+nl,n)
A(j,i:min(i+nu,n)) = A(j,i:min(i+nu,n)) - L(j,i)*U(i,i:min(i+nu,n));
end
U(i+1,i+1:min(i+1+nu,n)) = A(i+1,i+1:min(i+1+nu,n));
end