From 9aacb790fcc0233bd25d82eac59b42c4c3a15dc2 Mon Sep 17 00:00:00 2001 From: Charlotte Veitner Date: Tue, 28 Mar 2017 14:49:33 -0400 Subject: [PATCH] update --- lu_tridiag.m | 26 ++++++++++++++++++++++++-- solve_tridiag.m | 31 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 solve_tridiag.m diff --git a/lu_tridiag.m b/lu_tridiag.m index f147541..85367f9 100644 --- a/lu_tridiag.m +++ b/lu_tridiag.m @@ -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 diff --git a/solve_tridiag.m b/solve_tridiag.m new file mode 100644 index 0000000..3512bdc --- /dev/null +++ b/solve_tridiag.m @@ -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 + +