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?
linear_algebra/lu_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.
26 lines (22 sloc)
1.4 KB
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 [ud,uo,lo]=lu_tridiag(e,f,g); | |
%lu_tridiag The purpose of this function is to perform a LU-decomposition | |
%by creating a lower and upper matrix from three input vectors e,f,&g. This | |
%function is to provide a method that creates linear equations to compute | |
%the new matrix. | |
%Note: the inputs of the function must be vectors of the same length | |
%--------Linear equations----------- | |
ud = zeros(length(f), 1); %set up empty vector for new upper diagonal | |
uo = zeros(length(f)-1, 1); %empty vector for new upper-off diagonal matrix | |
lo = zeros(length(f)-1, 1); %empty vector for new lower-off diagonal matrix | |
lo = g; %The value of the original upper matrix equals the new lower-off diagonal | |
ud(1)=f(1); %first element in new upper diagonal equals first in original diagonal | |
uo(1) = g(1); %first element in new upper-off diagonal equals that of first in g | |
k = 2; %Set index to initialize while loop. | |
while k <= length(f)-1 %begin loop to assign values of generic vectors e f &g | |
lo(k-1) = e(k-1)/(ud(k-1)); %compute values for new lower-off digaonal | |
uo(k) = g(k); %assign values to new upper-off diagonal | |
ud(k) = f(k)-(lo(k-1)*uo(k-1)); | |
k = k + 1; %repeat but shift to next column | |
end | |
lo(k-1) = e(k-1)/(ud(k-1)); %Due to indexing loop, equation is needed for final value of new lower-off diagonal | |
ud(k) = f(k)-(lo(k-1)*uo(k-1)); %same reasoning, needed to find final value of new upper diagonal | |