| MATLAB Function Reference | ![]() |
Syntax
yi = interp1(x,Y,xi) yi = interp1(Y,xi) yi = interp1(x,Y,xi,method) yi = interp1(x,Y,xi,method,'extrap') yi = interp1(x,Y,xi,method,extrapval)
Description
傳回一個向量 yi = interp1(x,Y,xi)
yi ,向量 x 是資料點的x座標,向量 Y 是資料點的y座標,向量 xi 是內插點。若 Y 是一個矩陣,則會以 Y 的每一行來作內插,且 yi 是 length(xi)-by-size(Y,2)。
yi = interp1(Y,xi)
會假設 x = 1:N。 N 是向量 Y 的長度,或 size(Y,1) 。
可以指定內插的方法:yi = interp1(x,Y,xi,method)
'nearest' |
鄰近點內插法 |
'linear' |
線性內插法 (預設值) |
'spline' |
三次 spline 內插法 |
'pchip' |
Piecewise cubic Hermite 內插法 |
'cubic' |
(與 'pchip' 一樣) |
'v5cubic' |
MATLAB 第 5 版中的三次內插法 |
對於 'nearest'、 'linear' 和 'v5cubic' 這些方法, interp1(x,Y,xi,method) 會傳回 NaN 給由 x 生成的區間以外的元素 xi 之任何元素。而其他方法, interp1 則會用外插的方式來處理這些值。
yi = interp1(x,Y,xi,method,'extrap') 用特定的方法去對範圍之外的數值進行外插。
yi = interp1(x,Y,xi,method,extrapval) 範圍以外的值將傳回純量 extrapval 。 NaN 和 0 經常會代換成 extrapval。
interp1 指令會在資料點間進行內插。如下圖所示,向量 x 、 Y 、 xi 和 yi 之間的關係:
內插相當於查表的運作 (table lookup)。在對照表中, table 為 [x,Y] , interp1 會根據位置在 x 的元素中查詢 xi ,傳回 Y 中經由內插後的元素 yi 。
Examples
Example 1. 在 finer abscissa 上產生一個近似 sine 的曲線。
x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi); plot(x,y,'o',xi,yi)
Example 2. 有兩個向量分別表示年份(1900~1990)以及在美國相對應的人口數(百萬)。
t = 1900:10:1990;
p = [75.995 91.972 105.711 123.203 131.669...
150.697 179.323 203.212 226.505 249.633];
interp1(t,p,1975) 會用內插法來估計 1975 年的人口,答案如下:
ans =
214.8585
我們可以用內插法來估計 1900 到 2000 的人口,結果如下:
x = 1900:1:2000; y = interp1(t,p,x,'spline'); plot(t,p,'o',x,y)
如果資料都儲存在同一個表中的話,有的時候用查表法去推想內插法會比較方便些。若一部份的年份資料儲存在一個 5-by-2 的表如下:
tab =
1950 150.697
1960 179.323
1970 203.212
1980 226.505
1990 249.633
欲查詢 1975 年的人口,在矩陣 tab 中,可以用查表的方法
p = interp1(tab(:,1),tab(:,2),1975)
p =
214.8585
Algorithm
interp1 指令是一個 M-file。 'nearest' 和 'linear' 方法都可以很容易的去使用。
對於 'spline' 這個方法, interp1 會呼叫一個函式 spline ,這個函式會用到 ppval 、 mkpp 和 unmkpp 的運算,會與 piecewise polynomials 一起運作。 spline 用使用它們來產生三次 spline 內插法。若想得到進一步的資訊,可以參考 spline M-file ,和 Spline Toolbox。
對於 'pchip' 和 'cubic' 的方法, interp1 會呼叫 pchip 函式,產生 piecewise cubic interpolation 於向量 x 和 y 中。這個方法會保留單調性(monotonicity)及資料的狀態(shape)。可以參考 pchip 獲得更多的資訊。
See Also
interpft, interp2, interp3, interpn, pchip, spline
References
[1] de Boor, C., A Practical Guide to Splines, Springer-Verlag, 1978.
| int8, int16, int32 | interp2 | ![]() |