Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ME3255S2017/lecture_10/Tridiag.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
22 lines (22 sloc)
542 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function x = Tridiag(e,f,g,r) | |
% Tridiag: Tridiagonal equation solver banded system | |
% x = Tridiag(e,f,g,r): Tridiagonal system solver. | |
% input: | |
% e = subdiagonal vector | |
% f = diagonal vector | |
% g = superdiagonal vector | |
% r = right hand side vector | |
% output: | |
% x = solution vector | |
n=length(f); | |
% forward elimination | |
for k = 2:n | |
factor = e(k)/f(k-1); | |
f(k) = f(k) - factor*g(k-1); | |
r(k) = r(k) - factor*r(k-1); | |
end | |
% back substitution | |
x(n) = r(n)/f(n); | |
for k = n-1:-1:1 | |
x(k) = (r(k)-g(k)*x(k+1))/f(k); | |
end |