MATLAB HELP. Do not use any built in MATLAB functions such as lu, mldivide, and
ID: 3687765 • Letter: M
Question
MATLAB HELP. Do not use any built in MATLAB functions such as lu, mldivide, and inv THAT ARE NOT USED IN THE CODE PROVIDED, YOU CAN USE THE BUILT IN FUNCTIONS THAT ARE ALREADY SHOWN IN THE CODE BELOW (Don't copy and paste other chegg answers, they are all wrong)
BASED ON THE MATLAB FUNCTION [L,U] = get_lu(A) BELOW edit and form the function [L,U,P] = get_lu(A) that accepts a square matrix A and returns the LU decomposition of A with the permutation matrix P. If A is rectangular or singular, then you must notify the user by throwing a suitable error message using MATLAB function error or assert. Your function must perform row exchanges, if necessary, and the pivots must be chosen to reduce roundoff error using the technique of partial pivoting. (Hint: You’ll need to set a tolerance to determine when a pivot position value is “zero” and when A is singular.)
function [L,U] = get_lu(A)
[rows,cols] = size(A);
tol = 1.e-6;
for c = 1:cols
if abs(A(c,c)) < tol
disp(['Cannot continue: Small pivot (close to 0) encountered in column ' int2str(c) '.'])
end
L(c,c) = 1;
for r = (c + 1):rows
L(r,c) = A(r,c)/A(c,c);
for j = (c + 1):cols
A(r,j) = A(r,j) - L(r,c)*A(c,j);
end
end
for j = c:cols
U(c,j) = A(c,j);
end
end
Explanation / Answer
function [L,U] = get_lu(A)
[rows,cols] = size(A);
tol = 1.e-6;
for c = 1:cols
if abs(A(c,c)) < tol
disp(['Cannot continue: Small pivot (close to 0) encountered in column ' int2str(c) '.'])
end
L(c,c) = 1;
for r = (c + 1):rows
L(r,c) = A(r,c)/A(c,c);
for j = (c + 1):cols
A(r,j) = A(r,j) - L(r,c)*A(c,j);
end
end
for j = c:cols
U(c,j) = A(c,j);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.