I'm trying to LU decompose, with pivoting, the following matrix ($A=(a_{ij})$):
A = [2 1 2; 1 0 3; 4 -3 -1]; % matlab
I cannot make out from my literature (Chapra, "Applied Numerical Methods") or elsewhere in what order things should be done. The exercise I'm trying to do says to "Gauss eliminate using pivoting". I arrive at the same solution as Matlab
[L,U,P]=lu(A);
% here, L = [1 0 0; 0.5 1 0; 0.25 0.3 1]
% U = [4 -3 -1; 0 2.5 2.5; 0 0 2.5]
% P = [0 0 1; 1 0 0; 0 1 0]
if I first switch rows 1 and 3, then 2 and 3 to get
PA = [4 -3 -1; 2 1 2; 1 0 3]; % var name, not multiplication
upon which I perform Gauss elimination, putting $\ell_{21} = 2/4$, $\ell_{31}=1/4$ into $L=(\ell_{ij})$. However that is not how Chapra himself describes Gauss elimination with pivoting, seen here (fig 9.5, p 264 slider enumeration; without the back substitution).
Using his algorithm (applied here as: switch rows 1 and 3, gauss elim, switch rows 2 and 3, gauss elim), I get the same $U$ as with the above Matlab command, except that in my case,
K = [1 0 0; 0.25 1 0; 0.5 0.3 1]; % i.e. my decomp is K*U
However, $K*U \neq P*A$ with the above $P$. This is all a big mess. I've gone through Chapra's algorithm many times by now and I'm sure I've followed the algorithm correctly (point of the exercise is to do LU factorization by hand), what am I doing wrong? What is the correct way to do it? I'm afraid I don't find Chapra's explanations and examples very enlightening...