Im just trying to figure out how I would use the Interp1 function with the spline method and the Interp1 withe cubic method for approximation. Are the Interp1 function and some call to spline linked in the same code.
For example how would you approximate this data using the Interp1 function and spline method/ whats the difference between the cubic method.
year=[1;2;3;4;5;6;7;8;9;10];
pop=[5;10;15;20;25;30;35;40;50;60];

 채택된 답변

Matt J
Matt J 2019년 11월 1일
편집: Matt J 2019년 11월 1일

1 개 추천

how would you approximate this data using the Interp1 function and spline method
For example,
year=[1;2;3;4;5;6;7;8;9;10];
pop=[5;10;15;20;25;30;35;40;50;60];
interp1(year,pop, 2.5,'spline')
whats the difference between the cubic method.
It's just a different interpolation kernel. The cubic method will gives an interpolated curve that is only once-differentiable, but it will follow the shape of the data more closely than a spline interpolation. Example:
y=[zeros(1,10) 1 1.15 1.15 1 zeros(1,10)];
x=1:numel(y);
xq=linspace(1,numel(y),1000);
plot(x,y,'o',xq,interp1(y,xq,'spline'), xq, interp1(y,xq,'cubic'))
legend('', 'Spline','Cubic')

댓글 수: 4

Hi than you so much, does it make a difference if I have Nan data in the pop would it be
year=[1;2;3;4;5;6;7;8;9;10];
pop=[5;Nan;15;20;Nan;30;Nan;40;50;60];
NotMissing=~isnan(pop);
newp=pop(NotMissing,1);
newyear=year(NotMissing,1);
vq1 = interp1(newyear,newp,year,'spline');
Matt J
Matt J 2019년 11월 1일
편집: Matt J 2019년 11월 1일
You will probably get a warning that the NaNs will be ignored by interp1. The solution you've shown would avoid the warning. You could do the whole thing in one line, however, using fillmissing,
vq1=fillmissing( pop, 'spline','SamplePoints',year);
Hi thanks Im trying to graph it as well as you have but mine looks mess, I'm trying but it doesnt look like yours. Thanks again!
year=[1;2;3;4;5;6;7;8;9;10];
pop=[5;nan;15;20;nan;30;nan;40;50;60];
NotMissing=~isnan(pop);
newp=pop(NotMissing,1);
newyear=year(NotMissing,1);
vq1 = interp1(newyear,newp,year,'spline');
plot(newyear,newp,'o',year,interp1(newp,year,'spline'), year, interp1(newp,year,'cubic'))
legend('', 'Spline','Cubic')
Matt J
Matt J 2019년 11월 1일
편집: Matt J 2019년 11월 1일
This might be what you want
year=[1;2;3;4;5;6;7;8;9;10];
pop=[5;nan;15;20;nan;30;nan;40;50;60];
newp = fillmissing( pop, 'pchip','SamplePoints',year);
yq=linspace(1,10,100);
plot(year,newp,'o',...
yq,interp1(newp,yq,'spline'),'x',...
yq, interp1(newp,yq,'cubic'));
legend('Data', 'Spline/Pchip','Cubic/Pchip')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Splines에 대해 자세히 알아보기

질문:

2019년 11월 1일

편집:

2019년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by