4. (Partial Pivoting: MATLAB program) write a programa to find the LU partial pi
ID: 3908547 • Letter: 4
Question
4. (Partial Pivoting: MATLAB program) write a programa to find the LU partial pivoting. The program should return the updated matrix A and the pivot vector p. In MATLAB, name your file mylu.m, the first few lines of whi should be as follows: decomposition of a given n x n matrix A using function [a,p]-mylu(a) [n n]=size (a); p= (1 :n)'; (your code here !) The code above sets n equal to the dimension of the matrix and initializes the pivot vector p. Make sure to store the multipliers my in the proper matrix en- tries. For more help on function m-files see pages 9-13 of the MATLAB Primer by Kermit Sigmon available from the course webpage. You should experiment with a few small matrices to make sure your code is correct. Check if matrices resulting in LU decomposition satisfy PA LU. As a test of your code, in MATLAB execute the statements >diary mylu.txt >>format short e >type mylu.m >>a-t2 2-3:3 1-2:6 8 1); >>[a,pl-mylu(a) >>diary off Print and hand-in the text file containing your program.Explanation / Answer
Matlab code
=========
function [a,P]=mylu(A)
% LU factorization with partial (row) pivoting
[n,n]=size(A);
L=eye(n); P=L; U=A;
for k=1:n
[pivot m]=max(abs(U(k:n,k)));
m=m+k-1;
if m~=k
% interchange rows m and k in U
temp=U(k,:);
U(k,:)=U(m,:);
U(m,:)=temp;
% interchange rows m and k in P
temp=P(k,:);
P(k,:)=P(m,:);
P(m,:)=temp;
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end
end
for j=k+1:n
L(j,k)=U(j,k)/U(k,k);
U(j,:)=U(j,:)-L(j,k)*U(k,:);
end
a=U;
for i=2:n
for j=1:i-1
a(i,j)=L(i,j);
end
end
end
=========
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.