Skip to content

Commit

Permalink
slightly faster/shorter algorithm, updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaskis committed Dec 14, 2021
1 parent 6946b6b commit e14748b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Binary file modified EXAMPLES.mlx
Binary file not shown.
Binary file modified Tridiagonal_Matrix_Algorithm__Thomas_Algorithm_.pdf
Binary file not shown.
30 changes: 13 additions & 17 deletions tridiagonal.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
%
% x = tridiagonal(A,d)
%
% See also inv, gaussian_elimination.
% See also \, mldivide, /, mrdivide, inv, inv, gaussian_elimination.
%
% Copyright © 2021 Tamas Kis
% Last Update: 2021-08-28
% Last Update: 2021-12-14
% Website: https://tamaskis.github.io
% Contact: [email protected]
%
Expand Down Expand Up @@ -37,37 +37,33 @@
function x = tridiagonal(A,d)

% determines n
n = size(A,1);
n = length(d);

% preallocates all necessary vectors
a = zeros(n-1,1);
b = zeros(n,1);
c = zeros(n-1,1);
x = zeros(n,1);

% extracts "a" from "A"
% extracts first element of "b" from "A"
b(1) = A(1,1);

% forward loop
for i = 2:n

% extract relevant elements of "a", "b", and "c" from "A"
a(i-1) = A(i,i-1);
end

% extracts "b" from "A"
for i = 1:n
b(i) = A(i,i);
end

% extracts "c" from "A"
for i = 2:n
c(i-1) = A(i-1,i);
end

% forward elimination
for i = 2:n

% forward elimination
w = a(i-1)/b(i-1);
b(i) = b(i)-w*c(i-1);
d(i) = d(i)-w*d(i-1);

end

% backward substitution
% backward loop (backward substitution)
x(n) = d(n)/b(n);
for i = (n-1):(-1):1
x(i) = (d(i)-c(i)*x(i+1))/b(i);
Expand Down

0 comments on commit e14748b

Please sign in to comment.