Using Matlab, Write a function MYLU to perform LU factorization for an arbitrary
ID: 3886521 • Letter: U
Question
Using Matlab, Write a function MYLU to perform LU factorization for an arbitrary n×n matrix (under the assumption that elimination can be performed without row exchanges). Run the function on the input (Please base the code off of the templet shown below)
Thanks
% MYLU-The function in this m-file computes an LU factorization % of an n x n matrix under the assumption that elimination can be % performed without row exchanges. % Input: n x n matrix A. % Output: lower triangular matrix L and upper triangular matrix U. function [L,U] = MYLU (A) n = L = U % Get the row or column dimension of A. % Initialize L. % Initialize U. % Perform elimination. % The nested for loops fix a column first, and then go to each row. for j= % Span certain columns of A. for i= % span certain rows of A. L( ) = U ) = % Do not forget the semicolons here % to suppress intermediate output! end end endExplanation / Answer
function [L, U, P] = myLU4(A)
%A must be square
%get size of input matrix
sizeofA=size(A);
n=sizeofA(1);
%instantiate L, P, U
L=eye(n);
P=eye(n);
U=A;
% process
for i=1:n
%Row Reduction
if U(i,i) == 0
maxofU = max(abs(U(i:end,1)));
for j=1:n
if maxofU == abs(U(j,i))
temp = U(1,:);
U(1,:) = U(j,:);
U(j,:) = temp;
temp = P(:,1);
P(1,i) = P(j,:);
P(j,:) = temp;
end
end
end
if U(i,i) ~=1
temp = eye(n);
temp(i,i)=U(i,i);
L=L*temp;
U(i,:) = U(i,:)/U(i,i); %Ensure pivots are 1
end
if i~=n
for k=i+1:length(U)
temp = eye(n);
temp(k,i) = U(k,i);
L=L*temp;
U(k,:)=U(k,:)-U(k,i)*U(i,:);
end
end
end
P = P';
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.