Using MatLab: 1. Finish the following M-file. Run your function on the same matr
ID: 3666680 • Letter: U
Question
Using MatLab:
1. 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
2. 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
T=
3 9 5 7 1
8 8 6 0 5
2 2 4 2 4
1 7 0 4 6
4 2 7 5 5 .
Explanation / Answer
1.
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.