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));

Answers (1)

Walter Roberson
Walter Roberson on 3 Aug 2018
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.

Categories

Find more on Elementary Math in Help Center and File Exchange

Tags

Asked:

on 4 Apr 2012

Answered:

on 3 Aug 2018

Community Treasure Hunt

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

Start Hunting!