Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chv14004 committed Mar 28, 2017
1 parent 4aba4d0 commit 9aacb79
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lu_tridiag.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@

function [ud,uo,lo]=lu_tridiag(e,f,g)
function [ud,uo,lo]=lu_tridiag(e,f,g);
% lu_tridiag calculates the components for LU-decomposition of a tridiagonal matrix
% given its off-diagonal vectors, e (a) and g (b)
% and diagonal vector f (d)
% the output is
% the diagonal of the Upper matrix, ud (dd)
% the off-diagonal of the Upper matrix, uo
% and the off-diagonal of the Lower matrix, lo (bb)
% note: the diagonal of the Lower matrix is all ones
[m n]=size(f);
ud=zeros(m,n);
uo=zeros(m,n);
lo=zeros(m,n);
%ud(1)=f(1);
%lo(1)=0;
%uo=e;


%for i=2:n
% lo(i)=g(i)/(ud(i-1));
% ud(i)=f(i)-(lo(i)*e(i-1));
%end



n=size(e);

end
31 changes: 31 additions & 0 deletions solve_tridiag.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

function x=solve_tridiag(ud,uo,lo,b);
% solve_tridiag solves Ax=b for x
% given
% the diagonal of the Upper matrix, ud
% the off-diagonal of the Upper matrix, uo
% the off-diagonal of the Lower matrix, lo
% the vector b
% note: the diagonal of the Lower matrix is all ones

[s n]=size(uo);
z=zeros(s,n);
z(1)=b(1);
x=zeros(s,n);


%solve Lz=b using forward subsitution
for i= 2:n
z(i)=b(i)-(lo(i)*z(i-1));
end

%solve Ux=z using backward substitution
x(n)=z(n)/ud(n);

for k= (n-1):-1:1

x(k)=(z(k)-uo(k)*x(k+1))/ud(k);
end
end


0 comments on commit 9aacb79

Please sign in to comment.