matlab插值:已知数据x=[0,3,5,7,9,11,12,13,14,15] y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6]采用 最邻近插值 线性插值 三次样条插值 三次差值4种方法进行插值 要得到每隔0.5的数据《matlab基础教程》这个书上

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/03 17:47:10
matlab插值:已知数据x=[0,3,5,7,9,11,12,13,14,15] y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6]采用 最邻近插值 线性插值 三次样条插值 三次差值4种方法进行插值 要得到每隔0.5的数据《matlab基础教程》这个书上

matlab插值:已知数据x=[0,3,5,7,9,11,12,13,14,15] y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6]采用 最邻近插值 线性插值 三次样条插值 三次差值4种方法进行插值 要得到每隔0.5的数据《matlab基础教程》这个书上
matlab插值:已知数据x=[0,3,5,7,9,11,12,13,14,15] y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6]
采用 最邻近插值 线性插值 三次样条插值 三次差值4种方法进行插值 要得到每隔0.5的数据《matlab基础教程》这个书上的程序是:
%interpolation using the four methods
x=[0,3,5,7,9,11,12,13,14,15];
y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6];
length of x=length (x);
scalar x=[x(1):0.5:x(length of x)];
length of sx=length (scalar x);
for i=1:length of x;
y nearest(i)=interp1(x,y,scalar x(i),'nearest');
y linear(i)=interp1(x,y,scalar x(i),'linear');
y spline(i)=interp1(x,y,scalar x(i),'spline');
y cubic(i)=interp1(x,y,scalar x(i),'cubic');
end
subplot(2,2,1),plot(x,y,'*'),hold on,plot(scalar x,y nearest),title('method=nearest');
subplot(2,2,2),plot(x,y,'*'),hold on,plot(scalar x,y linear),title('method=linear');
subplot(2,2,3),plot(x,y,'*'),hold on,plot(scalar x,y spline),title('method=spline');
subplot(2,2,4),plot(x,y,'*'),hold on,plot(scalar x,y cubic),title('method=cubic');
为什么matlab显示这个程序错误?我用的是2010a

matlab插值:已知数据x=[0,3,5,7,9,11,12,13,14,15] y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6]采用 最邻近插值 线性插值 三次样条插值 三次差值4种方法进行插值 要得到每隔0.5的数据《matlab基础教程》这个书上
其实很简单,你只要删除很多空格即可比如 length of x=length(x)改成lengthofx=length(x).其他一样.
代码
x=[0,3,5,7,9,11,12,13,14,15];
y=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.4,1.6];
lengthofx=length (x);
scalarx=[x(1):0.5:x(lengthofx)];
lengthofx=length (scalarx);
 for i=1:lengthofx;
ynearest(i)=interp1(x,y,scalarx(i),'nearest');
ylinear(i)=interp1(x,y,scalarx(i),'linear');
yspline(i)=interp1(x,y,scalarx(i),'spline');
ycubic(i)=interp1(x,y,scalarx(i),'cubic');
end
subplot(2,2,1),plot(x,y,'*'),hold on,plot(scalarx,ynearest),title('method=nearest');
subplot(2,2,2),plot(x,y,'*'),hold on,plot(scalarx,ylinear),title('method=linear');
subplot(2,2,3),plot(x,y,'*'),hold on,plot(scalarx,yspline),title('method=spline');
subplot(2,2,4),plot(x,y,'*'),hold on,plot(scalarx,ycubic),title('method=cubic');
复制上述指令我也画出图来了,看来最小临近法插值效果不平滑