% LU factorization of a tridiagonal n by n matrix A

% Diagonal part of A is b(1), ..., b(n)
% Vector above the main diagonal of A is c(1),...,c(n-1)
% Vector below the main diagonal of A is a(2),...,a(n)

% L has a main diagonal part of 1s.
% Vector below the main diagonal of L is m(2),...,m(n)

% Diagonal part of U is d(1),...,d(n)
% Vector above the main diagonal of U is c(1),...c(n-1)
% K. Ming Leung, 02/05/03

% The following example is for matrix:
% A = [ 2 -1  0  0
%      -1  2 -1  0
%       0 -1  2 -1
%       0  0 -1  2];
clear all;
b=[2 2 2 2]; c=[-1 -1 -1]; a=[0 c]; n=length(b);
m=zeros(n,1); d=zeros(n,1); 

d(1)=b(1);
for i=2:n
    m(i)=a(i)/d(i-1);
    d(i)=b(i)-m(i)*c(i-1);
end;
display(m); display(d);