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/solve_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.
103 lines (67 sloc)
1.81 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 [ x ] = solve_tridiag(dU,odL,odU,b) | |
%UNTITLED2 Summary of this function goes here | |
% Detailed explanation goes here | |
% dU: upper matrix diagonal | |
% odU: upper matrix diagonals, order 4, 8, 7 | |
% odL: diagonal of lower matrix, order: 2, 6, 3 | |
% b: 3x1 matrix | |
% for general case | |
upper_diag = diag(dU); | |
width = length(upper_diag); | |
if width == 3 % for function we made | |
L = eye(3) + diag(odL(1:2),-1) + diag(odL(3),-2); % creates L matrix | |
U = upper_diag + diag(odU(1:2),1) + diag(odU(3),2); % creates U matrix | |
% for forward substitution | |
for i = 1:width | |
x(i) = b(i); | |
for k = 1:i-1 | |
x(i) = x(i) - L(i,k)*x(k); | |
end | |
x(i) = x(i)/L(i,i); | |
end | |
b = x'; | |
% for backwards substitution | |
for i = width:-1:1 | |
x(i) = b(i); | |
for k = i+1:width | |
x(i) = x(i) - U(i,k)*x(k); | |
end | |
x(i) = x(i)/U(i,i); | |
end | |
x = x'; | |
else % for A matrices | |
U = diag(dU); | |
k = 1; | |
for i = 1:width-1 | |
ofd = odU(k:k+width-i-1); | |
U = U + diag(ofd, i); | |
k = k + width - i; | |
end | |
% makign L matrix | |
L = eye(width); | |
k = 1; | |
for i = 1:width-1 | |
ofdL = odL(k:k+width-i-1); | |
L = L + diag(ofdL, -i); | |
k = k + width - i; | |
end | |
% for forward substitution | |
for i = 1:width | |
x(i) = b(i); | |
for k = 1:i-1 | |
x(i) = x(i) - L(i,k)*x(k); | |
end | |
x(i) = x(i)/L(i,i); | |
end | |
b = x'; | |
% for backwards substitution | |
for i = width:-1:1 | |
x(i) = b(i); | |
for k = i+1:width | |
x(i) = x(i) - U(i,k)*x(k); | |
end | |
x(i) = x(i)/U(i,i); | |
end | |
x = x'; | |
end | |
end |