Adding Permutation Matrix Into LU Factorization

조회 수: 10 (최근 30일)
Kyle Fertig
Kyle Fertig 2017년 12월 3일
Hello, I am working to create a function to do LU Factorization with partial pivoting. Through watching some videos, I created the code below, but I forgot to make the code return the permutation matrix and am having trouble adding it into the code. Any help with this would be greatly appreciated!
Thanks,
-Kyle
function f = mylu(A,b)
%%LU Decomposition
%Get Augmented Matrix
Ab=[A,b]
n = length(A);
L = eye(n);
P=eye(n)
%With A(1,1) as pivot Element
%Row exchange to make sure A(1,1) is the last in Column
col1=Ab(:,1);
[temp,idx] = max(col1);
temp = Ab(1,:);
Ab(1,:) = Ab(idx,:);
Ab(idx,:) = temp;
%Computation in the pivot column
for i=2:3;
alpha = Ab(i,1) / Ab(1,1);
L(i,1) = alpha;
Ab(i,:) = Ab(i,:) - alpha*Ab(1,:);
end
%With A(2,2) as pivot Element
%Row exchange to ensure A(2,2) is the largest in column 2
col2 = Ab(2:end,2);
[temp,idx] = max(col2);
temp = Ab(2,:);
Ab(2,:) = Ab(idx,:);
Ab(idx,:) = temp;
for i=3;
alpha = Ab(i,2) / Ab(2,2);
L(i,2) = alpha
Ab(i,:) = Ab(i,:) - alpha*Ab(2,:);
end
U=Ab(1:n,1:n)
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by