-
Notifications
You must be signed in to change notification settings - Fork 1
/
tridiagonal_vector.m
66 lines (60 loc) · 1.67 KB
/
tridiagonal_vector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%==========================================================================
%
% tridiagonal_vector Solves the tridiagonal linear system Ax = d for x
% using the vector implementation of the tridiagonal matrix algorithm.
%
% x = tridiagonal_vector(a,b,c,d)
%
% Copyright © 2021 Tamas Kis
% Last Update: 2022-10-22
% Website: https://tamaskis.github.io
% Contact: [email protected]
%
% TECHNICAL DOCUMENTATION:
% https://tamaskis.github.io/files/Tridiagonal_Matrix_Algorithm.pdf
%
%--------------------------------------------------------------------------
%
% ------
% INPUT:
% ------
% a - ((n-1)×1 double) tridiagonal vector
% b - (n×1 double) tridiagonal vector
% c - ((n-1)×1 double) tridiagonal vector
% d - (n×1 double) vector
%
% -------
% OUTPUT:
% -------
% x - (n×1 double) solution of the tridiagonal linear system Ax = d
%
% -----
% NOTE:
% -----
% --> The vectors a, b, and c define the diagonals of the tridiagonal
% matrix, A, as shown below:
%
% ⌈ ⋱ ⋱ ⌉
% | ⋱ ⋱ c |
% A = | ⋱ b ⋱ |
% | a ⋱ ⋱ |
% ⌊ ⋱ ⋱ ⌋
%
%==========================================================================
function x = tridiagonal_vector(a,b,c,d)
% determines n
n = length(d);
% preallocates solution vector
x = zeros(n,1);
% forward elimination
for i = 2:n
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
x(n) = d(n)/b(n);
for i = (n-1):(-1):1
x(i) = (d(i)-c(i)*x(i+1))/b(i);
end
end