The following material was produced via the diary command of MATLAB.
% We illustrate the construction of the LU factorization
% of a 2-by-2 matrix with row reduction as you would do by hand
A = [1 2
3 4]
A =
1 2
3 4
% we make the matrix A upper triangular,
% subtracting 3 times the first row from the second row:
U = A;
U(2,:) = U(2,:) - 3*U(1,:)
U =
1 2
0 -2
% this row reduction is represented by a multiplier matrix
M = eye(2); % 2-by-2 identity matrix
M(2,1) = -3;
M*A
ans =
1 2
0 -2
% so the inverse of M is the lower triangular matrix L
% of the LU factorization, but no inverse operation is needed:
L = eye(2);
L(2,1) = 3;
L*U
ans =
1 2
3 4
% If we now pick a right hand side vector, say b = [0 2],
% then solving A*x = b amounts to solving two triangular systems
b = [0 2]'
b =
0
2
% first we solve L*y = b, followed by U*x = y:
y = L\b
y =
0
2
x = U\y
x =
2
-1
A*x
ans =
0
2
% Note that the built-in command for LU factorization
% produces a different answer than the one we obtained:
[l,u] = lu(A)
l =
0.3333 1.0000
1.0000 0
u =
3.0000 4.0000
0 0.6667
L % the L we computed
L =
1 0
3 1
U
U =
1 2
0 -2
% What happened? Well, MATLAB permuted the rows:
[l,u,p] = lu(a)
l =
1.0000 0
0.3333 1.0000
u =
3.0000 4.0000
0 0.6667
p =
0 1
1 0
% We will see in later lectures why pivoting is needed.