Finish the following M-file. Run your function on the same matrix A as given in the lab.
% LU4 - The function in this M-file computes an LU factorization
% of a 4 x 4 matrix under the assumption that elimination can be
% performed without row exchanges. % Input: 4 x 4 matrix A;
% Output: lower triangular matrix L and upper triangular matrix U.
function [L,U] = LU4(A)
L = eye(4);
U = A;
for j= ...
for i= ...
L(...) = ...; % Do not forget the semicolons here
U(...) = ...; % to suppress intermediate output!
end
end
end
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
Get Answers For Free
Most questions answered within 1 hours.