Adding Permutation Matrix Into LU Factorization

3 次查看(过去 30 天)
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 中查找有关 Eigenvalues & Eigenvectors 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by