光滑和更精确。
如曲线拟合一样,插值要作决策。根据所作的假设,有多种插值。而且,可以在一维以上空间中进行插值。即如果有反映两个变量函数的插值,z=f(x, y),那么就可在x之间和在y之间,找出z的中间值进行插值。MATLAB在一维函数interp1和在二维函数interp2中,提供了许多的插值选择。其中的每个函数将在下面阐述。
为了说明一维插值,考虑下列问题,12小时内,一小时测量一次室外温度。数据存储在两个MATLAB变量中。
Temperature3530? hours=1:12; % index for hour data was recorded
? temps=[5 8 9 15 25 29 31 30 22 25 27 24]; % recorded
temperatures
? plot(hours, temps, hours, temps,' + ') % view temperatures ? title(' Temperature ')
? xlabel(' Hour '), ylabel(' Degrees Celsius ')
Degrees Celsius2520151050246Hour81012 图11.4 在线性插值下室外温度曲线
正如图11.4看到的,MATLAB画出了数据点线性插值的直线。为了计算在任意给定时间的温度,人们可试着对可视的图作解释。另外一种方法,可用函数interp1。
interp1的缺省用法是由interp1(x, y, xo)来描述,这里x是独立变量(横坐标),y是应变量(纵坐标),xo是进行插值的一个数值数组。另外,该缺省的使用假定为线性插值。 若不采用直线连接数据点,我们可采用某些更光滑的曲线来拟合数据点。最常用的方法是用一个3阶多项式,即3次多项式,来对相继数据点之间的各段建模,每个3次多项式的头两个导数与该数据点相一致。这种类型的插值被称为3次样条或简称为样条。函数interp1也能执行3次样条插值。
? t=interp1(hours, temps, 4.7, ' spline ') % estimate temperature at hour=4.7 t = 22.3143
? t=interp1(hours, temps, 9.3, ' spline ') % estimate temperature at hour=9.3 t = 21.8577
? t=interp1(hours, temps, [3.2 6.5 7.1 11.7]) % find temp at many points! t = 10.2000 30.0000 30.9000 24.9000
? t=interp1(hours, temps, 4.7) % estimate temperature at hour=4.7 t = 22
? t=interp1(hours, temps, 9.3) % estimate temperature at hour=9.3 t = 22.9000
? t=interp1(hours, temps, [3.2 6.5 7.1 11.7], ' spline ') t = 9.6734 30.0427 31.1755 25.3820
注意,样条插值得到的结果,与上面所示的线性插值的结果不同。因为插值是一个估计或猜测的过程,其意义在于,应用不同的估计规则导致不同的结果。
一个最常用的样条插值是对数据平滑。也就是,给定一组数据,使用样条插值在更细的间隔求值。例如,
在图11.5中,虚线是线性插值,实线是平滑的样条插值,标有' + '的是原始数据。如要求在时间轴上有更细的分辨率,并使用样条插值,我们有一个更平滑、但不一定更精确地对温度的估计。尤其应注意,在数据点,样条解的斜率不突然改变。作为这个平滑插值的回报,3次样条插值要求更大量的计算,因为必须找到3次多项式以描述给定数据之间的特征。关于样条的更详细信息可见下一章。
? xlabel(' Hour '), ylabel(' Degrees Celsius ') ? title(' Springfield Temperature ')
? plot(hours, temps, ' - ' , hours, temps, ' + ' , h, t) % plot comparative results ? t=interp1(hours, temps, h, ' spline ') ;
? h=1:0.1:12; % estimate temperature every 1/10 hour
Springfield Temperature3530Degrees Celsius2520151050246Hour81012 图11.5 在不同插值下室外温度曲线
在讨论二维插值之前,了解interp1所强制的二个强约束是很重要的。首先,人们不能要求有独立变量范围以外的结果,例如,interp1(hours, temps, 13.5)导致一个错误,因为hours在1到12之间变化。其次,独立变量必须是单调的。即独立变量在值上必须总是增加的或总是减小的。在我们的例子里,hours是单调的。然而,如果我们已经定义独立变量为一天的实际时间,
则独立变量将不是单调的,因为time_of_day增加到12,然后跌到1,再然后增加。如果用time_of_day代替interp1中的hours,将会返回一个错误。同样的理由,人们不能对temps插值来找出产生某温度的时间(小时),因为temps不是单调的。
? time_of_day=[7:12 1:6] % start at 7AM,end at 6PM time_of_day =
7 8 9 10 11 12 1 2 3 4 5 6
11.3 二维插值
二维插值是基于与一维插值同样的基本思想。然而,正如名字所隐含的,二维插值是对两变量的函数z=f(x, y) 进行插值。为了说明这个附加的维数,考虑一个问题。设人们对平板上的温度分布估计感兴趣,给定的温度值取自平板表面均匀分布的格栅。 采集了下列的数据:
如同在标引点上测量一样,矩阵temps表示整个平板的温度分布。temps的列与下标depth或y-维相联系,行与下标width或x-维相联系(见图11.6)。为了估计在中间点的温度,我们必须对它们进行辨识。
? title( [' Temperature at Depth = ' num2str(d) ] ) ? xlabel(' Width of Plate '), ylabel(' Degrees Celsius ') ? plot(wi, zlinear, ' - ' , wi, zcubic) % plot results
? zcubic=interp2(width, depth, temps, wi,d, ' cubic ') ; % cubic interpolation ? zlinear=interp2(width, depth, temps, wi, d) ; % linear interpolation ? d=2; % at a depth of 2
? wi=1:0.2:5; % estimate across width of plate
? temps=[82 81 80 82 84; 79 63 61 65 81; 84 84 82 85 86] % temps =
82 81 80 82 84 79 63 61 65 81 84 84 82 85 86 temperature data
? depth=1:3; % index for depth of plate (i,e,,the y-dimension) ? width=1:5; % index for width of plate (i.e.,the x-dimension)