如何用MATLAB 编写interpl插值函数

如何用MATLAB 编写interpl插值函数,第1张

yi

=

interp1(x,y,xi,method)

已知样本点坐标x,y,求xi处的函数值yi,冲胡插值方法是棚判衡method

method有以下几种:

'nearest'邻近点插值

'linear'线性链做插值(默认)

'spline'三次样条函数插值

'cubic'三次函数插值

常用的是'spline'和'cubic'

例子:

x

=

0:10

y

=

sin(x)

xi

=

0:.25:10

yi

=

interp1(x,y,xi,'spline')

plot(x,y,'o',xi,yi)

其实很简单,你只要物拿删除很多空格即可比如 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')

复制上述指令我也画出图来了,看来最小临近法插值效果不平滑


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12495507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存