Skip to content

Commit e14748b

Browse files
committed
slightly faster/shorter algorithm, updated documentation
1 parent 6946b6b commit e14748b

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

EXAMPLES.mlx

64 Bytes
Binary file not shown.
18.4 KB
Binary file not shown.

tridiagonal.m

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
%
66
% x = tridiagonal(A,d)
77
%
8-
% See also inv, gaussian_elimination.
8+
% See also \, mldivide, /, mrdivide, inv, inv, gaussian_elimination.
99
%
1010
% Copyright © 2021 Tamas Kis
11-
% Last Update: 2021-08-28
11+
% Last Update: 2021-12-14
1212
% Website: https://tamaskis.github.io
1313
1414
%
@@ -37,37 +37,33 @@
3737
function x = tridiagonal(A,d)
3838

3939
% determines n
40-
n = size(A,1);
40+
n = length(d);
4141

4242
% preallocates all necessary vectors
4343
a = zeros(n-1,1);
4444
b = zeros(n,1);
4545
c = zeros(n-1,1);
4646
x = zeros(n,1);
4747

48-
% extracts "a" from "A"
48+
% extracts first element of "b" from "A"
49+
b(1) = A(1,1);
50+
51+
% forward loop
4952
for i = 2:n
53+
54+
% extract relevant elements of "a", "b", and "c" from "A"
5055
a(i-1) = A(i,i-1);
51-
end
52-
53-
% extracts "b" from "A"
54-
for i = 1:n
5556
b(i) = A(i,i);
56-
end
57-
58-
% extracts "c" from "A"
59-
for i = 2:n
6057
c(i-1) = A(i-1,i);
61-
end
62-
63-
% forward elimination
64-
for i = 2:n
58+
59+
% forward elimination
6560
w = a(i-1)/b(i-1);
6661
b(i) = b(i)-w*c(i-1);
6762
d(i) = d(i)-w*d(i-1);
63+
6864
end
6965

70-
% backward substitution
66+
% backward loop (backward substitution)
7167
x(n) = d(n)/b(n);
7268
for i = (n-1):(-1):1
7369
x(i) = (d(i)-c(i)*x(i+1))/b(i);

0 commit comments

Comments
 (0)