关于MATLAB 插值(Interpolation)
(2008-06-23 20:03:55)
标签:
matlab插值interpolation一维二维杂谈 |
分类: 专业知识 |
插值:在已知数据之间计算估计值的过程。
1. 一维插值(1D Interpolation)
由interp1实现,用多项式技术计算插值点。
Yi=interp1(x,y,xi,method)
y—函数值矢量, x—自变量取值范围,xi—插值点的自变量矢量,Method—插值方法选项。
MATLAB6.1的4种方法:
*临近点插值:method= ‘nearest’
*线性插值:
*三次样条插值:method= ‘spline’
*立方插值:
选择插值方法时主要考虑因素:运算时间、占用计算机内存和插值的光滑程度。
下面,对四种插值法进行比较:
*临近点插值:
*线性插值:
*三次样条插值:
*立方插值:
例1:一维插值函数插值方法的对比。
x=0:10;
y=sin(x);
xi=0:0.25:10;
strmod={'nearest', 'linear', 'spline', 'cubic'} %将插值方法定义为单元数组
str1b={'(a) method=nearest', '(b) method=linear',...
'(c) method=spline', '(d)
method=cubic'}
for i=1:4
subplot(2,2,i)
plot(x,y, 'ro' ,xi,yi, 'b'),xlabel(str1b(i))
end
strmod =
例2: 三次样条插值
x0=0:10;
y0=sin(x0);
x=0:.25:10;
y=spline(x0,y0,x);
plot(x0,y0,'or',x,y,'k')
与interp1结果一样
2. 二维插值(2D Interpolation)
用于图形图象处理和三维曲线拟合等领域,由interp2实现,一般格式为:
ZI=interp2(X,Y,Z,XI,YI,method)
*临近点插值:method= ‘nearest’
*线性插值:
*三次样条插值:method= ‘spline’
*立方插值:
例 :二维插值4种方法的对比。
[x,y,z]=peaks(7); figure(1),
[xi,yi]=meshgrid(-3:0.2:3,-3:0.2:3);
z1=interp2(x,y,z,xi,yi,'nearest');
z2=interp2(x,y,z,xi,yi,'linear');
z3=interp2(x,y,z,xi,yi,'spline');
z4=interp2(x,y,z,xi,yi,'cubic');
figure(2),
title('nearest')
subplot(2,2,2)
mesh(xi,yi,z2)
subplot(2,2,3)
title('spiine')
subplot(2,2,4)
mesh(xi,yi,z4)
3. 多维插值: (3D Interpolation)
包括三维插值函数interp3和多维插值函数interpn,函数调用格式与一、二维插值基本相同。
VI=interp3(X,Y,Z,V,XI,YI,ZI,method)
其中: X,Y,Z—自变量组成的数组;
(FLOW A simple function of 3 variables.
(SLICE(X,Y,Z,V,XI,YI,ZI) draws slices through the volume V along the
(SHADING FLAT sets the shading of the current graph to flat.
例:三维插值实例。
[x,y,z,v]=flow(10);
figure(1),
slice(x,y,z,v,[6
[xi,yi,zi]=meshgrid(.1:.25:10,-3:.25:3,-3:.25:3);
vi=interp3(x,y,z,v,xi,yi,zi);
figure(2),
slice(xi,yi,zi,vi,[6
shading flat
FLOW A simple function of 3 variables.
FLOW, a function of three variables, is the speed
profile of a submerged jet within a infinite
tank.
There are several variants of the calling sequence: