Task 3: Create a Function for Crout (LU) Reduction (10 pts) Do not use any of MA
ID: 3817640 • Letter: T
Question
Task 3: Create a Function for Crout (LU) Reduction (10 pts) Do not use any of MATLAB's built in functions or commands. This MUST implemented using matrix and vector components via FOR loops in your construction. Use Crout reduction to convert a square matrix A into the product of triangular matrices LU. Use the MATLAB commands L zeros(n) and UE eye(n) to initialize matrices L and U, and then preform a reduction by implementing the algorithm for k 1:n for i k:n LU for j k+1:n k j where A is a square matrix of dimension ngn, L will be a lower-triangular matrix of dimension nen and U will be an upper-triangular matrix of like dimension neAn. No pivoting is to be programmed into your code. This function must have the interface CL, UE crout LU (A) and it needs to verify that A is a square nAn matrix with numeric elements, where n is arbitrary; if not your code should terminate with an error. It should write a warning message to the command window if any diagonal element of the reduced matrix L is zero valued.Explanation / Answer
%Square array a
A = [1 2; 3 4]
%function performing crout reduction
function [L, U] = crout_lu(A)
n = size(A)(1);
L = zeros(n);
U = eye(n);
for k = 1:n
for i = k:n
sum = 0;
j = k - 1;
for m = 1:j
sum = sum + L(i, m) * U(m, k);
end
L(i, k) = A(i, k) - sum;
end
for i = k+1:n
j = k - 1;
sum = 0;
for m = 1:j
sum = sum + L(k, m)*U(m, j);
end
U(k, i) = (A(k, i) - sum)/L(k, k);
end
end
%checking if any diagonal element in l is 0
for k = 1:n
if L(k, k) == 0
disp('Warning, diagonal element in L is zero');
end
end
end
%function to test whether the matrix a is a proper one or not(ie. square, numeric)
function res = isproper(A)
res = 1;
if isnumeric(A) == 0
res = 0;
end
if size(A)(1) != size(A)(2)
res = 0;
end
if res == 0
disp('error in matrix a');
end
end
%first check if the matrix is proper
res = isproper(A);
%if the matrix is proper, perform crout reduction
if (res == 1)
[L, U] = crout_lu(A)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.