function [L,U,P] = lu_pivot(A)
n = size(A)*[1,0]';
U = A;
L = eye(n);
P = eye(n);
for k = 1 : n-1
    [x,i] = max(abs(U(k:n, k)));
    U([k i], k : n) = U([i k], k : n);
    L([k i], 1 : k - 1) = L([i k], 1 : k - 1);
    P([k i], :) = P([i k], :);
    for j = k+1 : n
        L(j,k) = U(j,k) / U(k,k);
        U(j, k : n) = U(j, k : n) - L(j, k)*U(k, k : n);
    end
end
end