how to interpolate the non uniform resolution in cubic spline?

4 次查看(过去 30 天)
Hi all, if I have 6 segments with resolution of 8,8,8,8,8,12, why can not cubic spline interpolate correctly for the last segment? How to solve this problem? Thank you very much
  2 个评论
John D'Errico
John D'Errico 2017-12-15
编辑:John D'Errico 2017-12-15
Why can't you? Because you did not write the correct code. Sorry, but the computation is trivial with a cubic spline, written correctly to handle unequal spacing between the breaks. (Triviality is sometimes in the eye of the beholder.)
What did you do wrong? That is impossible to know, because we cannot magically see inside your computer, and you did not provide your code.
Most likely, you assumed that the data spacing was uniform, and wrote the equations and code to follow that assumption. So don't do it that way.
If this is for homework, then the issue is you need to write code using the correct equations. If this is for your work or whatever task you are trying to perform, then DON'T WRITE YOUR OWN SPLINE CODE. Use existing tools like spline, or pchip, or interp1, etc.
AniCnx
AniCnx 2017-12-17
编辑:AniCnx 2017-12-17
Thank you for your nice comments, John D'Errico. actually, i did code it on my own as following and i attached the result as pic. in pic, that can be seen the last segment has error of interpolation.
%if true
seg=24;
NL=[10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;17];
Ck=[-266.479532163743,-266.479532163743,-236.267393053600,-162.405313757590,-92.1402364342393,-27.3010252757420,10.5878240764911,37.7567786138143,50.6856443813328,48.3565527346632,41.6192148682732,38.9397293502820,12.0864493612835,-8.65639937685077,12.7171882144171,51.7106955326762,42.4350761397639,112.616420850404,119.405044382449,81.1969741122724,26.0127908204208,-20.8259537029191,-43.0898340533435,-6.51334559323291,-8.89813958239892,-11.2829335715649,-11.2829335715649,-11.2829335715649];
x=1;
for s=1:seg;
for k=1:NL(s,1);
%cubic spline
y=x/NL(s,1); fx=floor(y)+1;
for p=-2:1; BV(p+3,1)=B3spline(y-(fx+p)); end %compute BV
Gcs(x,1)=Ck(1,fx:fx+3)*BV;
x=x+1;
end
end
%for Ck plot
A=zeros(1,seg);
for i=2:seg;
temp=NL(i,1);
A(1,i)=A(1,i-1)+temp;
end
plot(Gcs(:,1))
hold on
plot(A,Ck(1,2:25,1),['*','r']);
function B=B3spline(x) x=abs(x); if x<1 B=(2/3) - (x*x) + (x*x*x)/2; elseif x>=1 && x<2 B=(x-2)*(x-2)*(x-2)/(-6); else B=0; end %end

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2017-12-15
See my attached spline demo. Of course, you can put in any x value you want to get the estimated/spline value at that location.
  1 个评论
AniCnx
AniCnx 2017-12-17
Thank you for your suggestion, Image analysis. in case of your demo, i quite pretty understand that. but i face to the problem as each segment has non-uniform resolution as my result is wrong interpolation as shown in picture.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by