From: Jason Fender on
Hello,

I'm trying to use Newton's Divided Difference on 21 data points. I think my code is good for the most part, however, when I go to plot, I get a strange problem. It plots my data points correctly, however, depending on what number I put in for the interval between points on x, it blows up my graph. Here is the line I'm talking about: x=[-1:1/1000:1];. With the 1/1000 in there, it makes the plot 1000 times bigger than it should be. If I change that number to 1/10, it makes it 10 times bigger. Also, I'm noticing that the shape of the plot is correct, but it's not actually plotting on the points. It's starting at x=1 and plotting from there out. Hopefully with the full code it's easier to understand. I'm terrible at explaining things.

clear
clc
clf

xx = -1:1/100:1;
X = [-1.0000:0.1:1.0000];
Y = [2.3504,1.8030,1.3692,1.0197,0.7357,0.5052,0.3214,0.1802,0.0800,0.0200,0,0.0200,0.0800,0.1802,0.3214,0.5052,0.7357,1.0197,1.3692,1.8030,2.3504];
n=length(X);
N=length(xx);
z=zeros(N,1);

if n~=length(Y)
error('X and Y must be the same length.');
end

y=Y(1);
p=1;
for i=1:(n-1);
for j=1:(n-i);
Y(j)=(Y(j+1)-Y(j))/(X(j+i)-X(j));
end
CE(i)=Y(1);
end
CE1=CE(1);
CE2=CE(2);
CE3=CE(3);
CE4=CE(4);

syms x

x_factor(1)=(x-X(1));
for i=2:(n-1)
x_factor(i)=((x_factor(i-1))*(x-X(i)));
end
x_factor;

P=y(1);
for i=1:(n-1);
P=P+(CE(i)*(x_factor(i)));
end
P1=factor(P)
x=[-1:1/1000:1];
v=subs(P1);

X = [-1.0000:0.1:1.0000];
Y = [2.3504,1.8030,1.3692,1.0197,0.7357,0.5052,0.3214,0.1802,0.0800,0.0200,0,0.0200,0.0800,0.1802,0.3214,0.5052,0.7357,1.0197,1.3692,1.8030,2.3504];

figure(1)
plot(X,Y,'*')
hold on
plot(v,'r')
legend('Given Data','Fit Curve')
hold off

Thanks for any help that anyone can provide.

- Jason