Write a function in MATLAB that takes as input an n×1 vector p of rearranged int
ID: 3597918 • Letter: W
Question
Write a function in MATLAB that takes as input an n×1 vector p of rearranged integers from 1 to n representing a permutation matrix P whose i-th row is the p(i)-th row of the identity matrix; an n×n matrix B whose upper triangular portion stores U and strictly lower triangular portion stores L of the LU factorization of the matrix P A; and an n×1 vector b. Have this function output the solution to Ax = b. Use only basic programming. Only submit the code, but to make sure your code works, if you input p = [3; 1; 2], B = [2, 1, 3; 0.4, 3, 3; 0.5, 0.2, 4] and b = [2; 1; 1], you should output x = [0.53; 0.83; 0.30].
Explanation / Answer
%find x
function x=findX(n, p, B,b)
%create A
A=zeros(n,n);
%use loop to get A
for kk=1:n
%Find kkth row of A
A(kk,:)=B(p(kk),:);
%end
end
%find x
x=A
%end of findX()
%sample run
p = [3;1;2] ;
B = [2 -1 3;-0.4 -3 3;0.5 -0.2 4] ;
b = [2;-1;1];
%call to findX()
x=findX(3, p, B, b)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.