Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB HELP. Do not use any built in MATLAB functions such as lu, mldivide, and

ID: 3687598 • 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

BASED ON THE MATLAB FUNCTION [L,U] = get_lu(A) BELOW 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

LU matrix factorization

The lu function expresses a matrix A as the product of two essentially triangular matrices, one of them a permutation of a lower triangular matrix and the other an upper triangular matrix. The factorization is often called the LU, or sometimes the LR, factorization. A can be rectangular.

Y = lu(A) returns matrix Y that contains the strictly lower triangular L, i.e., without its unit diagonal, and the upper triangular U as submatrices. That is, if [L,U,P] = lu(A), then Y = U+L-eye(size(A)). The permutation matrix P is not returned.

[L,U] = lu(A) returns an upper triangular matrix in U and a permuted lower triangular matrix in L such that A = L*U. Return value L is a product of lower triangular and permutation matrices.