Octave で関数の内挿

# Octave で関数の内挿

## 1.　一次元の内挿 `interp1()`

    usage:
    yi = interp1(x, y, xi)
    yi = interp1(y, xi)
    yi = interp1(..., method)
    yi = interp1(..., extrap)
    yi = interp1(..., "left")
    yi = interp1(..., "right")
    pp = interp1(..., "pp")

x, y: データポイント。x が省略されたときには `x = 1:length(y)` と解釈される

xi, yi: 内挿されたデータポイント

method: `"nearest"`, `"previous"`, `"next"`, `"linear"`(デフォルト), `"pchip"`, `"cubric"`, `"spline"`

extrap: `"extrap"` 終点を超えて外挿する
        数値 終点以降に指定された数値で外挿する
        NA デフォルト

`"right"`, `"left"`: 不連続点の指定

`"pp"`: 内挿値は返さず，ピースワイズ多項式オブジェクトを返す


```matlab
xf = [0:0.05:10];
yf = sin(2*pi*xf/5);
xp = [0:10];
yp = sin(2*pi*xp/5);
lin = interp1(xp, yp, xf);
near = interp1(xp, yp, xf, "nearest");
pch = interp1(xp, yp, xf, "pchip");
spl = interp1(xp, yp, xf, "spline");
plot(xf, yf, "r", xf, near, "g", xf, lin,"b", xf, pch, "c", xf, spl, "m",
    xp, yp, "r*");
legend("original", "nearest", "linear", "pchip", "spline");
```



![output_2_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/65e371a3-9979-8742-9e9d-265592a83bf2.png)




```matlab
x = [0:0.05:10];
y = sin(2 * pi * x/5);
xi = 0:10;
interp1(x, y, xi, "linear", "extrap")
```

    ans =
    
     Columns 1 through 8:
    
            0   0.9511   0.5878  -0.5878  -0.9511  -0.0000   0.9511   0.5878
    
     Columns 9 through 11:
    
      -0.5878  -0.9511  -0.0000
    


上と同じことを `interp1()` で `"pp"` を指定し，戻り値を使って `ppval()` により `xi` のときの `y` を求めることで行う。


```matlab
pp = interp1(x, y, "linear", "pp");
ppval(pp, xi)
```

    ans =
    
     Columns 1 through 8:
    
            0   0.9511   0.5878  -0.5878  -0.9511  -0.0000   0.9511   0.5878
    
     Columns 9 through 11:
    
      -0.5878  -0.9511  -0.0000
    


## 2.　二次元の内挿 `interp2()`

    usage:
    zi = interp2(x, y, z, xi, yi)
    zi = interp2(z, xi, yi)
    zi = interp2(z, n)
    zi = interp2(z)
    zi = interp2(..., method)
    zi = interp2(..., method, extrap)

method: `"nearest"`, `"linear"`(デフォルト), `"pchip"`, `"cubic"`, `"spline"`


```matlab
clf;
 colormap("default");
 a = [13, -1, 12; 5, 4, 3; 1, 6, 2];
 x = [0, 1, 4];
 y = [10, 11, 12];
 xi = linspace(min(x), max(x), 17);
 yi = linspace(min(y), max(y), 26)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "linear"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;

```


    
![output_7_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/b23e9b9b-5bf9-f5b5-6309-e6ccba674493.png)

    



```matlab
clf;
 colormap("default");
 [x, y, a] = peaks(10);
 x = x(1, :)';
 y = y(:, 1);
 xi = linspace(min(x), max(x), 41);
 yi = linspace(min(y), max(y), 41)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "linear"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_8_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/76bfec3c-0b3e-df60-f817-07346a851913.png)

    



```matlab
clf;
 colormap("default");
 a = [13, -1 , 12; 5, 4, 3; 1, 6, 2];
 x = [0, 1, 4];
 y = [10, 11, 12];
 xi = linspace(min(x), max(x), 17);
 yi = linspace(min(y), max(y), 26)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "nearest"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_9_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/cb60ad1b-07bd-9b2b-4153-636af9f3abec.png)

    



```matlab
clf;
 colormap("default");
 [x, y, a] = peaks(10);
 x = x(1, :)';
 y = y(:, 1);
 xi = linspace(min(x), max(x), 41);
 yi = linspace(min(y), max(y), 41)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "nearest"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_10_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/1c72b829-629f-eea4-c196-6e9a47551eea.png)

    



```matlab
clf;
 colormap("default");
 a = [13, -1, 12; 5, 4, 3; 1, 6, 2];
 x = [0, 1, 2];  y = [10, 11, 12];
 xi = linspace(min(x), max(x), 17);
 yi = linspace(min(y), max(y), 26)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "cubic"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_11_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/cb4fb9aa-67ea-304b-f7aa-1b26f855bd84.png)

    



```matlab
clf;
 colormap("default");
 [x, y, a] = peaks(10);
 x = x(1, :)';
 y = y(:, 1);
 xi = linspace(min(x), max(x), 41);
 yi = linspace(min(y), max(y), 41)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "cubic"));
 [x,y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_12_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/294d904a-c9f7-9305-2243-f23549aeef23.png)

    



```matlab
clf;
 colormap("default");
 a = [13, -1, 12; 5, 4, 3; 1, 6,2];
 x = [0, 1, 2];
 y = [10, 11, 12];
 xi = linspace(min(x), max(x), 17);
 yi = linspace(min(y), max(y), 26)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "spline"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_13_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/e8a01114-471b-fbd2-cb12-3f70fb83419a.png)

    



```matlab
 clf;
 colormap("default");
 [x, y, a] = peaks(10);
 x = x(1, :)';
 y = y(:,1);
 xi = linspace(min(x), max(x), 41);
 yi = linspace(min(y), max(y), 41)';
 mesh(xi, yi, interp2(x, y, a, xi, yi, "spline"));
 [x, y] = meshgrid(x, y);
 hold on; plot3(x, y, a, "b*"); hold off;
```


    
![output_14_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/ea966b98-ef7f-7bd5-e5a3-245a40406517.png)

    


## 3.　三次元の内挿 `interp3()`

    usage:
    vi = interp3(x, y, z, v, xi, yi, zi)
    vi = interp3(v, xi, yi, zi)
    vi = interp3(v, n)
    vi = interp3(v)
    vi = interp3(..., method)
    vi = interp3(..., method, extrapval)

method: `"nearest"`, `"linear"`(デフォルト), `"cubic"`, `"spline"`


```matlab
x = y = z = -1:1;
f = @(x, y, z) x.^2 - y - z.^2;
[xx, yy, zz] = meshgrid (x, y, z);
v = f(xx, yy, zz);
xi = yi = zi = -1:0.1:1;
[xxi, yyi, zzi] = meshgrid (xi, yi, zi);
vi = interp3 (x, y, z, v, xxi, yyi, zzi, "spline");
mesh (zi, yi, squeeze (vi(1, :, :)));
```


    
![output_16_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/7096f387-afff-2f8e-b5f3-90e436d8c677.png)

    


## 4.　$n$ 次元の内挿 `interpn()`

    usage:
    vi = interpn(x1, x2, ..., v, y1, y2, ...)
    vi = interpn(v, y1, y2, ...)
    vi = interpn(v, m)
    vi = interpn(v)
    vi = interpn(..., method)
    vi = interpn(..., method, extrapval)


```matlab
x = y = z = -1:1;
f = @(x, y, z) x.^2 - y - z.^2;
[xx, yy, zz] = meshgrid (x, y, z);
v = f(xx, yy, zz);
xi = yi = zi = -1:0.1:1;
[xxi, yyi, zzi] = ndgrid (xi, yi, zi);
vi2 = interpn (x, y, z, v, xxi, yyi, zzi, "spline");
mesh (zi, yi, squeeze (vi2(1, :, :)));
```


    
![output_18_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/e07280aa-00b0-dbc0-bc5d-7b7240bd808b.png)