加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

关于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= ‘linear’

*三次样条插值:method= ‘spline’

*立方插值:   method= ‘pchip’ or ‘cubic’

选择插值方法时主要考虑因素:运算时间、占用计算机内存和插值的光滑程度。

下面,对四种插值法进行比较:

                运算时间、  占用计算机内存      光滑程度。

*临近点插值:      快             少              

*线性插值:       稍长           较多             稍好

*三次样条插值:   最长           较多             最好

*立方插值:       较长            多              较好

 

例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

   yi=interp1(x,y,xi,strmod{i});

subplot(2,2,i)

plot(x,y, 'ro' ,xi,yi, 'b'),xlabel(str1b(i))

end

strmod =

 

    'nearest'    'linear'    'spline'    'cubic'

例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)    X,Y—自变量组成的数组,尺寸相同。

                                  XI,YI—插值点的自变量数组

                                  Method—插值方法选项,4种

*临近点插值:method= ‘nearest’

*线性插值:  method= ‘linear’     该方法是interp2函数的缺省方法

*三次样条插值:method= ‘spline’

*立方插值:   method= ‘pchip’ or ‘cubic’

例 :二维插值4种方法的对比。

[x,y,z]=peaks(7); figure(1),  mesh(x,y,z)

[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),  subplot(2,2,1)

 mesh(xi,yi,z1) 

title('nearest')

subplot(2,2,2) 

mesh(xi,yi,z2)

 title('linear')

subplot(2,2,3)

 mesh(xi,yi,z3) 

title('spiine')

subplot(2,2,4) 

mesh(xi,yi,z4)

 title('cubic')

3. 多维插值: (3D Interpolation)

包括三维插值函数interp3和多维插值函数interpn,函数调用格式与一、二维插值基本相同。

VI=interp3(X,Y,Z,V,XI,YI,ZI,method)

其中: X,Y,Z—自变量组成的数组;

       V—三维函数数组

       XI,YI,ZI—插值点的自变量数组

       Method  --插值方法选项。

(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.  FLOW is useful for

    demonstrating SLICE and INTERP3.)

(SLICE(X,Y,Z,V,XI,YI,ZI) draws slices through the volume V along the

    surface defined by the arrays XI,YI,ZI.)

(SHADING FLAT sets the shading of the current graph to flat.

    SHADING INTERP sets the shading to interpolated.

    SHADING FACETED sets the shading to faceted, which is the default.)

 

例:三维插值实例。

[x,y,z,v]=flow(10);      %三变量无限大容器淹没射流场的速度剖面图

figure(1),

slice(x,y,z,v,[6  9.5],2,[-2  .2])  %在X=6,9.5, Y=2, z=-2, 0.2五处取切面

[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  9.5],2,[-2  .2])     %切面插值

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.  FLOW is useful for demonstrating SLICE and INTERP3.

There are several variants of the calling sequence:

   V = FLOW; produces a 50-by-25-by-25 array.

   V = FLOW(N) produces a 2N-by-N-by-N array.

   V = FLOW(X,Y,Z) evaluates the speed profile at the points (X,Y,Z).

   [X,Y,Z,V] = FLOW(...) returns the coordinates as well.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有