Newton interpolation for 9 values

1 view (last 30 days)
Evangelos Rompogiannakis
Evangelos Rompogiannakis on 4 Dec 2020
Answered: Monisha Nalluru on 7 Dec 2020
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 Comments
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 4 Dec 2020
Because the time step is not constant, you should use divided difference not differences
Evangelos Rompogiannakis
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).

Sign in to comment.

Answers (1)

Monisha Nalluru
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:

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!