开发者

How is the Spline function different from the Interp1 function when using the Spline method?

开发者 https://www.devze.com 2023-03-28 15:01 出处:网络
Does anybody know why I would be getting different results from the Spline function vs. the Interp1 function using the Spline method?I have tried to look up if others have this problem but the only开发

Does anybody know why I would be getting different results from the Spline function vs. the Interp1 function using the Spline method? I have tried to look up if others have this problem but the only开发者_Python百科 thing that I can find is that the functions expect the inputs in different orders (i.e. column vs. row). The code that I am running is...

p1 = [20 40];    p2 = [200 500];    p3 = [400 300];    p4 = [600 500];
p = [p1; p2; p3; p4];

axis([0 1000 0 1000]); hold;
plot(p(:,1), p(:,2),'o')
x = linspace(0,1000,600);

%% 1
pp = spline(p(:,1),p(:,2));
yy = ppval(pp, x);
plot(yy,'r')

%% 2
y = interp1(p(:,1),p(:,2),x,'spline');
plot(x,y,'g')

If you look at the resulting plots, there is a large difference and I am unsure as to why that is. Any help would be appreciated!


You made a typing error in plotting the first method.

That should be:

%% 1
pp = spline(p(:,1),p(:,2));
yy = ppval(pp, x);
plot(x,yy,'r') %notice the x!

%% 2
y = interp1(p(:,1),p(:,2),x,'spline');
plot(x,y,'g')

Without that x parameter, the plot will take `x = 1:numel(yy)' on the x-axis of your plot, which causes the squeezed plot you had before.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号