tdma subscript zero problem

function x=TDMAsolver(a,b,c,d)
% a, b, c are coloumn vectors for compressed tridiagonal matrix, d is the
% right vector
%n is the number of rows
n = length(b);
% Modify the first row cofficients
c(1)=c(1)/ b(1); % Division by zero risk.
d(1)=d(1)/b(1); % division by zero would imply a singular matrix
for i=2:n-1;
temp=b(i)-a(i)*c(i-1);
c(i)=c(i)/temp;
d(i)=(d(i)-a(i)*d(i-1))/temp;
end
d(n)=(d(n)-a(n)*d(n-1))/(b(n)-a(n)*c(n-1));
%back to substitute
x(n)=d(n);
for i =n-1:-1:1
x(i)=d(i)-c(i)*x(i+1);
end
end
i have a problem in running this====
Attempted to access d(0); index must be a positive integer or logical.
Error in TDMAsolver (line 16)
d(n)=(d(n)-a(n)*d(n-1))/(b(n)-a(n)*c(n-1));

回答(1 个)

Walter Roberson
Walter Roberson 2018-8-3

0 个投票

If n = length(b) is 1, then when you try to access d(n-1) or c(n-1) in (d(n)-a(n)*d(n-1))/(b(n)-a(n)*c(n-1)) then you would have a problem.

类别

帮助中心File Exchange 中查找有关 Elementary Math 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by