0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Octave で関数の内挿

Posted at

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": 内挿値は返さず,ピースワイズ多項式オブジェクトを返す

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

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 を求めることで行う。

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"

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

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

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

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

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

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

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

 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

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"

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

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)
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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?