作业帮 > 综合 > 作业

matlab问题x=[0:0.2*pi:2*pi];%原始数据y=sin(x);xx=[0:0.01:2*pi];%需要

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/11 18:55:42
matlab问题
x=[0:0.2*pi:2*pi];%原始数据
y=sin(x);
xx=[0:0.01:2*pi];%需要插值的点
str=['linear';'nearest';'spline';'cubic'];
%str为插值字符串数组
for i=1:4%比较4种插值方法
yy=interp1(x,y,xx,str(i,:));%一维插值
figure(i);
plot(x,y,'g*',xx',sin(xx),xx,yy,'r-')
legend('data','y=sin(x)',str(i,:))
end
运行出现
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
试试下面的代码,即把str改为元胞数组,因为str=['linear';'nearest';'spline';'cubic'];字符串数组要求每个字符串长度一样,下边用到str时都采用元胞数组引用
x=[0:0.2*pi:2*pi];%原始数据y=sin(x);xx=[0:0.01:2*pi];%需要插值的点str={'linear';'nearest';'spline';'cubic'};
%str为插值字符串数组for i=1:4%比较4种插值方法 yy=interp1(x,y,xx,str{i});%一维插值 figure(i); plot(x,y,'g*',xx',sin(xx),xx,yy,'r-') legend('data','y=sin(x)',str{i});end