% LU factorization using Matlab's lu

%-------------------------------------------------------
% no pivoting
A=[-10 7 6 5; 4 3 -1 -7; 1 2 6 8; -2 -4 5 3]

[L,U]=lu(A)

b=[1; 0; -2; -9]
%b=[200; -37; pi; 17]

y=forsub(L,b)
x=backsub(U,y)

%-------------------------------------------------------
% with pivoting

% 3-component system illustrating round-off error
A = [10 -7 0; -3 2.09999 6; 5 -1 5]

[L,U]=lu(A)
[L,U,P]=lu(A)

b=[7; 3.90001; 6]

y=forsub(L,P*b)
x=backsub(U,y)

% 4-component system where pivoting is necessary
A=[1 2 3 -2; 2 4 1 0; 3 3 2 5; -1 6 2 1]

[L,U,P]=lu(A)

b=[5; 0; 4; -3]

y=forsub(L,P*b)
x=backsub(U,y)

A\b


