function x=tridiag(n,a,b,c,f)

% initialize
d=zeros(n,1);
u=zeros(n,1);
l=zeros(n,1);

% build L and U
i=1;

d(i)=b(i);
u(i)=c(i);

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

i=n;

    l(i)=a(i)/d(i-1);
    d(i)=b(i)-l(i)*u(i-1);
    
% solve L y = f
y=zeros(n,1);

y(1)=f(1);
for i=2:n
    y(i)=f(i)-l(i)*y(i-1);
    
end

% solve U x = y
x=zeros(n,1);

x(n)=y(n)/d(n);
for i=n-1:-1:1
    x(i)=(y(i)-u(i)*x(i+1))/d(i);
end

