Attempting to interpolate with interp2 and getting errors about the sample point vector?

Im trying to interpolate between 4 points in a square (origin to 1 in both x and y direction).
I used [x, y] = meshgrid(0:0.01:1, 0:0.01:1);
x = [0 1 1 0];
y = [0 0 1 1];
and now try to interpolate: interp2(x, y, strains, x, y, 'linear');
my matrix strains has an x and a y component for each of the 4 corners: [x1, y1; x2, y2, x3, y3; x4, y4]
I keep getting errors like: "Interpolation requires at least two sample points for each grid dimension."
what am I missing? It doesnt work if each point only has one strain value either

 Respuesta aceptada

% strains is defined on corners (0,0), (1,0), (1,1), (0,1), in that order
strains = [1 5; 2 6; 3 7; 4 8]
% points to interpolate to
[x, y] = meshgrid(0:0.01:1, 0:0.01:1);
% - use interpn for handling a multi-valued function (i.e., strains has
% m = 2 values at each point)
% - reorder rows of strains for interpolation, because interpn will
% interpret the order as (0,0), (1,0), (0,1), (1,1)
% - reshape the reordered strains as 2x2xm, to match the size of X and Y
X = [0 1];
Y = [0 1];
si = interpn(X, Y, reshape(strains([1 2 4 3],:),2,2,[]), x, y, 'linear');
figure
subplot(2,1,1)
pcolor(x,y,si(:,:,1));
colorbar
subplot(2,1,2)
pcolor(x,y,si(:,:,2));
colorbar
If you run that, you'll see that the top pcolor plot has value 1 at corner (0,0), value 2 at corner (1,0), value 3 at corner (1,1) and value 4 at corner (0,1). Similarly the bottom pcolor plot as values 5, 6, 7, and 8 at those respective corners. This shows that the ordering is as specified in the question by x = [0 1 1 0]; y = [0 0 1 1];.

4 comentarios

Relevant section of interpn documentation describing V. Note in particular the case where V has more than n dimensions, where n is the number of grid vectors specififed:
Note that interp2 requires V to be a matrix (so up to 2d only), which implies it can't handle multi-valued functions.
Of course, you could construct a complex column vector from strains, where the columns of strains are used as the real and imaginary parts. Then you could use interp2 on that, since it accepts complex-valued V (with different reordering since interp2 uses meshgrid format but interpn uses ndgrid format).
temp = strains(:,1) + 1i*strains(:,2);
si_2 = interp2(X, Y, reshape(temp([1 4 2 3]),2,[]), x, y, 'linear');
% the real part of si_2 is equivalent to the first page of si (the 3d
% result), and the imaginary part of si_2 is equivalent to the second
% page of si, so si can be reconstructed as
si_new = cat(3,real(si_2),imag(si_2));
isequal(si_new,si) % it's the same as before using interpn
Check that you've defined everything the same way I have (the easiest way to do that would be to copy and paste the code). It runs for me (R2022a). Check in particular that you don't have an X and x mixed up or a Y and y.
Interpolated values of strain. Top plot is interpolated from the first column of strains; bottom is second column.
If using the simple strains values in my answer ([1 5; 2 6; 3 7; 4 8]), it's easy to verify that the corner ordering you specified ((0,0), (1,0), (1,1), (0,1), in that order) is respected.
J
J el 20 de Nov. de 2024
Editada: J el 20 de Nov. de 2024
thanks!

Iniciar sesión para comentar.

Más respuestas (1)

[x, y] = meshgrid(0:0.01:1, 0:0.01:1);
x = [0 1 1 0];
y = [0 0 1 1];
You are overwriting the grids of data produced by meshgrid() with your explicit assignment of vectors to x and y.
interp2(x, y, strains, x, y, 'linear')
This says that at x(J,K), y(J,K) the data is strains(J,K) and asks for linear interpolation at the exact same set of points, x(J,K) y(J,K) . interp2() does not do any kind of smoothing. If you ask for outputs at exactly the same place as your inputs, you are going to get the exact values copied out of strains (to within round-off error.) There is no point in doing interp2() for this situation.
Now, what would make more sense is if you were to use
[x, y] = meshgrid(0:0.01:1, 0:0.01:1);
X = [0 1 1 0];
Y = [0 0 1 1];
interp2(x, y, strains, X, Y, 'linear')
On the other hand, this is just going to reply with strains(1,1), strains(1,end), strains(end,end), strains(end,1)

1 comentario

yes my bad, I do have them differentiated:
interp2(x, y, strains, xq, yq, 'linear') - does the X go first or the x actually? I had it the other way around as you did now?

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Centro de ayuda y File Exchange.

Productos

Versión

R2023b

Etiquetas

Preguntada:

J
J
el 19 de Nov. de 2024

Editada:

J
J
el 20 de Nov. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by