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

11 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Voss
Voss am 19 Nov. 2024
% 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 Kommentare
Voss
Voss am 19 Nov. 2024
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 19 Nov. 2024
[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 Kommentar
J
J am 19 Nov. 2024
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by