I cannot use interp2 as MATLAB tells me my grid arrays do not have an NDGRID structure, but I am still able to do a surface plot with no errors.

81 Ansichten (letzte 30 Tage)
I am trying to conduct a 2D interpolation across a surface. I have thrust, velocity and power values at every 1000 RPM and I wish to output an interpolated power value using know thrust and velocity as inputs.
load 18x55.mat
T = 15;
V = 5;
for n = 1:1:9
Vrange(1:30,n) = eval(strcat('x18x55.V',string(n*1000),'(1:30)'));
Trange(1:30,n) = eval(strcat('x18x55.T',string(n*1000),'(1:30)'));
Prange(1:30,n) = eval(strcat('x18x55.P',string(n*1000),'(1:30)'));
RPM(n) = n*1000;
end
Pq = interp2(Trange, Vrange, Prange, T, V);
Error using griddedInterpolant
Grid arrays must have NDGRID structure.

Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});

Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
The attached 18x55 table contains 30 thrust, velocity and power values at every 1000 RPM, from 1000 to 9000 RPM. The column labelling format is as follows:
At 1000 RPM:
Velocity = V1000
Thrust = T1000
Power = P1000
This continues up to 9000 RPM.
When I try to use interp2, MATLAB tells me my grid arrays must have an NDGRID structure, but I'm not sure what this means, but if I try to do a surface plot as below, I have no issues:
surf(Trange,Vrange,Prange)
Any help is greatly appreciated.
  4 Kommentare
Jon
Jon am 12 Apr. 2023
Yes I understand the columns are for increments of 1000 rpm. What variable changes between row 1 of your table and row 2 of your table, row 3 of your table etc?
Ethan Smith
Ethan Smith am 12 Apr. 2023
the velocity increases non-uniformly with every row, resulting in different thrust and power values

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 12 Apr. 2023
Bearbeitet: Cris LaPierre am 12 Apr. 2023
EDIT: fixing variable assignment per @Jon's observations
An ndgrid means your data is regularly sampled in space. If you look at the examples shown for ndgrid and compare to yours, you should see what the difference is.
[X,Y] = ndgrid(1:2:11,2:2:12)
X = 6×6
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 11 11 11 11 11 11
Y = 6×6
2 4 6 8 10 12 2 4 6 8 10 12 2 4 6 8 10 12 2 4 6 8 10 12 2 4 6 8 10 12 2 4 6 8 10 12
You may therefore want to look into scatteredInterpolant.
load 18x55.mat
x18x55(end,:)=[];
Vrange = x18x55{:,1:3:end};
Prange = x18x55{:,2:3:end};
Trange = x18x55{:,3:3:end};
RPM = (1:9)*1000;
F = scatteredInterpolant(Trange(:),Vrange(:),Prange(:))
F =
scatteredInterpolant with properties: Points: [270×2 double] Values: [270×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
T = 15;
V = 5;
vq = F(T,V)
vq = 172.5887
  3 Kommentare
Jon
Jon am 12 Apr. 2023
Bearbeitet: Jon am 12 Apr. 2023
The above approach is correct, but there are some errors in the details.
First it looks like you only want to use the first 30 rows of data in your table, the above uses all of the rows.
Second the above swaps thrust and pressure when reading in the table values
Finally, I think you want to find an interpolated value of pressure given the thrust and velocity, so the output variable from the interpolation should be name pq, rather than vq
You can correct this modifying @Cris LaPierre's code as follows
load 18x55.mat
Vrange = x18x55{(1:30),1:3:end};
Prange = x18x55{(1:30),2:3:end};
Trange = x18x55{(1:30),3:3:end};
RPM = (1:9)*1000;
F = scatteredInterpolant(Trange(:),Vrange(:),Prange(:))
T = 15;
V = 5;
pq = F(T,V)
Jon
Jon am 12 Apr. 2023
Bearbeitet: Jon am 12 Apr. 2023
Also, for future reference, regarding your original code. It is generally better to avoid using the MATLAB eval function when possible. Please see https://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html
In your case you could avoid using the eval, by using a dynamic name for the column in your tables. So for example:
Vrange(:,n) = x18x55.(strcat('V',string(n*1000)))(1:30);
Also, I would comment that x18x55 is not a very nice variable name for a table. It is better practice to use variable names that give others (and maybe your future self when you go back to the code some time later) a name that suggest what is contained in the variable. So for example you could call the table engineData, rather than x18x55
Finally, if you assign variables in a loop, you should preallocate arrays to hold the values. This improves performance, and also avoid some possibly unexpected results if the variables already exist in the workspace.
So in your case
% Preallocate arrays to hold data
Vrange = zeros(30,9);
Trange = zeros(30,9);
Prange = zeros(30,9)
for n = 1:1:9
Vrange(1:30,n) = eval(strcat('x18x55.V',string(n*1000),'(1:30)'));
Trange(1:30,n) = eval(strcat('x18x55.T',string(n*1000),'(1:30)'));
Prange(1:30,n) = eval(strcat('x18x55.P',string(n*1000),'(1:30)'));
.
.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 12 Apr. 2023
Verschoben: Torsten am 12 Apr. 2023
NDGRID structure means:
If X and Y are two vectors X = [x1,x2,x3,...,xn], Y = [y1,y2,y3,...,ym] with x1 < x2 < ... < xn and y1 < y2 < ... < ym, then your Trange and Vrange matrices are defined as Trange(i,j) = x(i) for all j and Vrange(i,j) = y(j) for all i.
In simple words: Trange and Vrange define an orthogonal mesh over the rectangle [x1,xn] x [y1,ym].
If this is not the case for your data, use "ScatteredInterpolant":

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by