0

I'm writing a function to interpolate polynomials with the Newton method, but I'm having some trouble.

function n = newtonInter(x, y)
n = length(x);
a = zeros(n,1);

for k = 0:n-1
       a(k) = y(k);
       for i = k + 1:n
           y(i) = (y(i) - y(k))/(x(i)-x(k));
       end
end
a(n) = y(n);

I get an error code in line 5, "Subscript indices must either be real positive integers or logicals."

I set x = [1 2 3 4] and y = [2 0 -10 -34]

jqdc2224
  • 119
  • 9
  • Unlike `C`, `Java` etc, `Matlab` arrays are **not** [zero-based](https://en.wikipedia.org/wiki/Zero-based_numbering), which means that you have to change your outer loop to `for k = 1:n` and, of course, do the necessary changes to the rest of your code – sokin Nov 15 '15 at 07:03
  • @sokin Thanks! changing it to k=1:n did the trick, however I'm getting the wrong answer :\, trying to figure out what to change for the rest of the code now. Edit: Figured it out! thanks again :) – jqdc2224 Nov 15 '15 at 07:13
  • It's OK. Glad I helped. – sokin Nov 15 '15 at 07:17

0 Answers0