Understanding the Behavior of interp2 and interp3

15 visualizaciones (últimos 30 días)
Michael Boutros
Michael Boutros el 20 de Sept. de 2019
Respondida: Athul Prakash el 24 de Sept. de 2019
I'm finding that the behavior of interp2 and interp3 is counterintuitive, and I'd like to understand how properly to use them. Here's a simple example using interp2:
XX_grid = linspace(eps,10,10);
YY_grid = linspace(eps,50,10);
[XX,YY] = meshgrid(XX_grid,YY_grid);
V = rand(10,10);
x_i = 4; y_i = 9;
interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i)) % (a)
interp2(YY_grid,XX_grid,V,YY_grid(y_i),XX_grid(x_i)) % (b)
V(x_i,y_i)
I setup two interpolations, (a) and (b). To test the code, I call the interpolations at two points exactly on the grid, which should exactly match the value in the interpolated matrix, V. However, (a) does not match the value from V, while (b) does. (a) is the original way I'd setup the interpolation but it didn't work, and by experimenting, I found that reversing the order of X and Y in (b) does the trick.
I tried the same thing with interp3 and, again, the code doesn't work as I expect. However, because we're in 3D space now, it isn't valid to simply change the order of the grids (and Matlab rightfully throws an error). Can someone shed some light on how I can get this to work?
X_grid = linspace(eps,5,10);
Y_grid = linspace(eps,7,10);
Z_grid = linspace(eps,3,10);
[X,Y,Z] = meshgrid(X_grid,Y_grid,Z_grid);
V = rand(10,10,10);
x_i = 3; y_i = 10; z_i = 5;
interp3(X,Y,Z,V,X_grid(x_i),Y_grid(y_i),Z_grid(z_i))
V(x_i,y_i,z_i)
Thanks for any and all help!
  3 comentarios
Michael Boutros
Michael Boutros el 20 de Sept. de 2019
Yes, and this is why reversing X and Y yields the interpretation that I have in mind. However, I can't see how this translates to the 3D case.
Bjorn Gustavsson
Bjorn Gustavsson el 20 de Sept. de 2019
No. Reset.
This is showing that the interp2-function does as it should:
XX_grid = linspace(eps,10,10);
YY_grid = linspace(eps,50,12);
V = rand(12,10);
[XX,YY] = meshgrid(XX_grid,YY_grid);
x_i = 4; y_i = 9;
Vi = interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i));
[Vi V(y_i,x_i) (Vi-V(y_i,x_i))]
After your call to interp2 you should do the matlab-correct comparison.
If I recall correctly you can call interp3 with coordinate grids made with meshgrid and ndgrid. Chose wisely - I don't remember having problems with the selection I made, but your application might have different requirements. Just make sure you test your choise works afterwards - most plotting-functions etc works well with grids produced with meshgrid.
Check the outputs of those functions for small arrays to see the difference:
[Xm,Ym] = meshgrid(-1:2,0:2:4)
Xm =
-1 0 1 2
-1 0 1 2
-1 0 1 2
Ym =
0 0 0 0
2 2 2 2
4 4 4 4
[Xn,Yn] = ndgrid(-1:2,0:2:4)
Xn =
-1 -1 -1
0 0 0
1 1 1
2 2 2
Yn =
0 2 4
0 2 4
0 2 4
0 2 4
HTH

Iniciar sesión para comentar.

Respuesta aceptada

Athul Prakash
Athul Prakash el 24 de Sept. de 2019
Hey Michael,
Consider the syntax:
Vq = interp2(XX, YY, V, Xq, Yq);
The query point is interpreted in a graphical sense, therefore 'Xq' works along the horizontal direction (i.e. second dimension) of 'V' and 'Yq' is taken along the first dimension of 'V'. Therefore in your (a) line, you are asking for the point with co-ordinates (XX_grid(4), YY_grid(9)), which is mapped to the 4th horizontal and 9th vertical value in matrix 'V'. This is indexed as V(9, 4), since MATLAB treats first index as the row number (i.e along vertical dimension).
Hence,
interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i))
V(y_i, x_i)
these 2 lines give the same output.
If you want to interpolate 3D functions, the tricky part is still the same as before: y dimension (vertical) is the first index of the underlying matrix and x (horizontal) dimension if the second index. The z dimension is simply the last index. Hence, a graphical co-ordinate (x,y,z) would have indices V(y,x,z) for a 3D matrix V. [Swap x-y; z is the same]
In your example,
interp3(X,Y,Z,V,X_grid(x_i),Y_grid(y_i),Z_grid(z_i))
V(y_i, x_u, z_i)
these 2 lines would give the same output.
Hope it helps!

Más respuestas (0)

Categorías

Más información sobre Interpolating Gridded Data en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by