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

Need MATLAB code. Thanks! Write a MATLAB function of the form [L, U, P ] = get_l

ID: 3763793 • Letter: N

Question

Need MATLAB code. Thanks!

Write a MATLAB function of the form [L, U, P ] = get_lu (A) that accepts a square matrix A and returns the LU decomposition of A with the permutation matrix P. Your function must perform row exchanges, if necessary, and the pivots must be well-scaled (to reduce roundoff error) using the technique of partial pivoting. If A is singular or not square (i.e. rectangular), then you must notify the user by throwing a suitable error message using MATLAB function error or assert.

Explanation / Answer

function newgauss(A)
[rows,columns]=size(A);
P=eye(rows,columns);
if(det(A)==0)
disp('No solutions or infinite number of solutions')
return;
end
U=A;
L=eye(rows,columns);
pivot=1;
while(pivot<rows)
max=abs(U(pivot,pivot));
maxk=0;%%find maximum abs value in column pivot
for k=pivot+1:rows
if(abs(U(k,pivot))>max)
max=abs(U(k,pivot));
maxk=k;
end
end %%if needed then switch
if(maxk~=0)
temp=U(pivot,:);
U(pivot,:)=U(maxk,:);
U(maxk,:)=temp;
temp=P(pivot,:);
P(pivot,:)=P(maxk,:);
P(maxk,:)=temp;
end %%Grade the column pivot using gauss elimination
for i=pivot+1:rows
num=U(k,pivot)/U(pivot,pivot);
U(k,:)=U(k,:)-num*U(pivot,:);
L(k,pivot)=num;
end
pivot=pivot+1;
end
disp('PA is:');
disp(P*A);
disp('LU is:');
disp(L*U);
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote