C =



I’d like to offer two brief observations in light of the discussion above. In the formal derivation of tridiagonal systems—particularly those arising from PDE discretisations—every coefficient in the defining equations must be assigned a value, including those associated with the boundaries. The boundary conditions provide these values; if they are homogeneous, the corresponding couplings simply evaluate to zero.
From an implementation perspective, it is perfectly acceptable to omit terms for non-existent neighbours by treating their coefficients as zero. However, from a mathematical standpoint, defining them explicitly maintains clarity, preserves the structure of the recurrence relations, and makes later changes to the boundary conditions straightforward.
This supports the point raised that while certain couplings may appear “non-existent” in the physical mesh, they nonetheless have a defined mathematical role. Explicitly setting them—even to zero—keeps both the derivation and the computational implementation consistent.
Hi @Filip,
I’ve prepared the MATLAB script you asked for. It:
This should fully address your requirement of generating the matrix for arbitrary size without utilizing matlab toolbox.
Script
% --------------------------------------------------------- % Build V, s, and gamma for any number of elements n % from given vectors a, D and full coupling matrix C. % % Implements: % v(i,i-1) = -C(i,i-1) * a(i-1) % v(i,i) = (C(i,i-1) + C(i,i+1) + D(i)) * a(i) % v(i,i+1) = -C(i,i+1) * a(i+1) % % s(i) = -C(i,i+1)*(a(i+1)-a(i)) + C(i,i-1)*(a(i)-a(i-1)) % % gamma = V \ s % ---------------------------------------------------------
clear; clc;
% --- Example input (change as needed) --- n = 5;
a = [1.0; 0.95; 1.05; 0.9; 1.1]; D = [0.2; 0.15; 0.1; 0.25; 0.3];
C = zeros(n); % full coupling matrix C(1,2) = 1.2; C(2,1) = 1.2; C(2,3) = 0.8; C(3,2) = 0.8; C(3,4) = 1.0; C(4,3) = 1.0; C(4,5) = 0.7; C(5,4) = 0.7;
% --- Allocate --- V = zeros(n); s = zeros(n,1);
fprintf('--- Building V and s ---\n\n');
for i = 1:n % Get neighbor couplings (zero if none) C_left = 0; if i > 1, C_left = C(i, i-1); end C_right = 0; if i < n, C_right = C(i, i+1); end
% Fill V
if i > 1
V(i, i-1) = -C_left * a(i-1);
end
V(i, i) = (C_left + C_right + D(i)) * a(i);
if i < n
V(i, i+1) = -C_right * a(i+1);
end % Fill s
term1 = 0; if i < n, term1 = -C_right * (a(i+1) - a(i)); end
term2 = 0; if i > 1, term2 = C_left * (a(i) - a(i-1)); end
s(i) = term1 + term2; % Debug print
fprintf('Row %d:\n', i);
fprintf(' C_left = %g\n', C_left);
fprintf(' C_right = %g\n', C_right);
if i > 1, fprintf(' V(%d,%d) = %g\n', i, i-1, V(i,i-1)); end
fprintf(' V(%d,%d) = %g\n', i, i, V(i,i));
if i < n, fprintf(' V(%d,%d) = %g\n', i, i+1, V(i,i+1)); end
fprintf(' s(%d) = %g\n\n', i, s(i));
end% --- Final results ---
disp('Matrix V:'); disp(V);
disp('Vector s:'); disp(s);
gamma = V \ s;
disp('Gamma:'); disp(gamma);
Please see attached.


Hope this is what you’re looking for.
Hi @Walter,
That is really weird, thanks for letting me know. I do appreciate your help and feedback. Please see attached.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!