# Newton interpolation for 9 values

3 views (last 30 days)
Hello im trying to run an interpolation for the values shown below in the code but for a reason when i plot the results I get a curve that doesnt go through all the points. Can someone help me solve this problem plz?
X=[0 14 36 52 68 75 83 94 100]; % time (sec)
Y=[6000 5750 4530 3675 1762 1279 895 429 151]; % Range (meters)
n = length(X) % set n = 9
h = X(2)-X(1); % stepsize
d = zeros(n,n) % 9x9 matrix of zeros
d(:,1)= Y %
for j = 2:n
for i=j:n
d(i,j) = d(i,j-1)-d(i-1,j-1)
end
end
C = d(n,n);
for k = n-1:-1:1
p = poly(X(1))/h;
p(2) = p(2)-(k-1);
C = conv(C,p)/k;
m = length(C);
C(m) = C(m)+d(k,k);
end
A = polyval(C,n)
t = linspace(X(1),X(n),100)
y = polyval(C,t)
set(figure(1),'DefaultLineLineWidth',1.5)
plot(t,y,'b')
hold on
plot(X,Y,'ro')
Thank you !
##### 2 CommentsShowHide 1 older comment
Evangelos Rompogiannakis on 4 Dec 2020
Could you please give a hint on how to implement this in my code. I used the difference of X(1) and X(2).

Monisha Nalluru on 7 Dec 2020
Inorder to use divided difference in newton interpolation in the code
Refer the the corresponding x according to formula
As an example
for j = 2:n
for i=j:n
d(i,j) = (d(i,j-1)-d(i-1,j-1))/(X(i)-X(i-j+1));
end
end
Also refer the code from file example which can give you idea on iimplementation: