%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% PA=LU decomposition and 2-step Gaussian elimination in MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
len = 1e3;

%A = [2 4 -2; 4 9 -3; -2 -1 7]; b = [2 8 10]';
A = rand(len); b = rand(len,1);


disp('Gaussian elimination for Ax=b\n');
tic
x1 = A\b;
toc

fprintf('PA=LU, where P=permulation matrix\n'); 
tic
[L,U,P]=lu(A);
toc

fprintf('2-step Gaussian elimination for LUx=Pb using triangular matrices\n');
tic,
z = L\(P*b);
x2 = U\z;
toc

%c = [4 8 -6]';
c = rand(len,1);

fprintf('\n Gaussian elimination for Ay=c\n');
tic
y1 = A\c;
toc

fprintf('2-step Gaussian elimination for LUy=Pc using triangular matrices\n');
tic
z = L\(P*c);
y2 = U\z;
toc

