Reputation: 772
I know this type of question have been asked plenty of times before, but I cannot understand the problem with my code. Also I am absolute beginner in Octave.
function Z=nat_spline(n, T, Y)
for i=0:n-1
H(i) = T(i+1) - T(i);
B(i) = 6*(Y(i+1) - Y(i))/H(i);
end
U(1) = 2*(H(0)+H(1));
V(1) = B(1) - B(0);
for i=2:n-1
U(i) = 2 * (H(i) + H(i-1)) - ( (H(i-1))^2 / U(i-1) );
V(i) = B(i) - B(i-1) - H(i-1)*V(i-1)/U(i-1);
end
Z(n) = 0
for i=n-1:1
Z(i) = (V(i)-H(i)*Z(i+1))/U(i);
end
Z(0) = 0;
end
Its a short code, so I guess it would be easy to spot any mistake. Thanks a lot for any helps.
Upvotes: 0
Views: 1161
Reputation:
Well, the title tells it: if not logicals. subscripts must be strictly positive integer values. Things like H(0)
or Z(0)
won't work, because strictly positive integer values begin with 1.
Upvotes: 0
Reputation: 1447
index of array start with 1 in matlab :)
your H(i) and B(i) will be H(0) B(0) in the first iteration of the loop and that will give you an error
so for i=1:n
Upvotes: 3