% Script to test LU_factor.m for
% LU factorization of an n by n matrix A
% using Gauss elimination without pivoting
% LU_factort.m
% A is factored as A = L*U
% Output:
% L is lower triangular with the main diagonal part = 1s.
% U is upper triangular and is stored in the original mtx A
% K. Ming Leung, 01/26/03
% K. Ming Leung, 02/21/04 (version 2), more general tests.

clear all; format short g;
% This is example 2.13 on p.69 of Heath:
A = [1 2 2
     4 4 2
     4 6 4]; b=[3; 6; 10];
%A = [ 4 12 8 4; 1 7 18 9; 2 9 20 20; 3 11 15 14];  % Example on p.135 Fausett
n = 10; A = rand(n,n);  b = rand(n,1); % MATLAB's lu function will perform pivoting in general!

[n,n] = size(A);
[L,U] = LU_factor(A,n);
display(L);
display(U);
[LL,UU] = lu(A),

 y = forwardSubstitution(L,b,n);
 x = backSubstitution(U,y,n),
 xMatlab = A\b,
 [x xMatlab],
 max(abs(x-xMatlab)),
 