求助大神们,Matlab中LUP分解怎么写啊?? 不要(L,U,P)=lu(A)

2026年09月23日 23:23
有1个网友回答
网友(1):

这个你拿去试试看.
```matlab
function [L, U, P] = lu_pp(A)
% initialize L, U and P.
n = size(A, 1);
L = eye(n);
U = A; % set U equal to A
P = eye(n);
m = eye(n); % records the previous mi

for i=2:n
% STEP 1: find pi and update U
pi = eye(n); % initialize pi
% find the max row
max = abs(U(i-1, i-1));
max_ind=i-1;
for j=i:n
if abs(U(j, i-1))>max
max = abs(U(j, i-1));
max_ind = j;
end
end
% now we know the number of the max row, then get pi
pi([max_ind i-1], :) = pi([i-1 max_ind], :);
U = pi*U; % update U

% STEP 2: find mi
mi = eye(n); % initialize mi
for j=i:n % calculate mi
mi(j, i-1) = rdivide(-U(j, i-1), U(i-1, i-1));
end

U = mi*U; % STEP 3: update U

P = pi*P; % STEP 4: update p

% STEP 5: update l
if i==3
L = inv(pi*m*transpose(pi));
else
L = L*(inv(pi*m*transpose(pi)));
end

% update m
m = mi;
end
% multiply the final term
L = L*(inv(m));
end
```