How to Interpolate scalar on new grid
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MRobert
il 27 Dic 2021
Commentato: Star Strider
il 28 Dic 2021
Dear forum,
I have a Results.csv file which contains several data from a numerical simulation (3 columns with 200 rows):
- Column 1: X coordinate of the mesh/grid point;
- Column 2: Y coordinate of the mesh/grid point;
- Column 3: A scalar, let's say temperature.
I recreated the grid in Matlab version 2021b with the temperature results. Now I want to interpolate the Temperature on a finner grid. I attached a part of the code I used and the error I receive. The problem seems to be in declaring "xr" and "yr" as scatter(xr,yr) shows just a straight line. Could you please tell me what am I missing?
Thank you in advance,
Regards,
Robert
%%% Read coordinates %%%
x = Results(:,1);
y = Results(:,2);
T = Results(:,3);
[X,Y]=meshgrid(x,y);
Temp = griddata(x,y,T,X,Y,'linear')
figure;
surf(X,Y,Temp)
colormap(jet);
colorbar;
%%% Refine the existing grid %%%
xr = linspace(min(x), max(x), 1000);
yr = linspace(min(y), max(y), 1000);
[Xr,Yr] = meshgrid(xr,yr);
%%% interpolate Temp on the new grid %%%
IntTemp = interp2(X,Y,Temp,Xr,Yr)
%%% ERROR %%%
Error using griddedInterpolant
Sample points must be unique.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in untitled (line 50)
int=interp2(X,Y,IntTemp,Xr,Yr);
1 Commento
dpb
il 27 Dic 2021
As always and as the comments show, without an actual dataset it is very difficult to know how to solve a given problem. Attach the data as either a .mat file containing the needed variables or the the beginning data file.
One possibility that works in some cases is to introduce "jitter" that is sufficiently large enough so as to eliminate the duplicate values but still small enough so as to not markedly change the results. A few multiples of eps generally will remove the duplicate value warning.
Risposta accettata
Star Strider
il 27 Dic 2021
The most likely source of the problem is that ‘x’ and ‘y’ do not have unique values, so neither will ‘X’ and ‘Y’ since they are derived from them. Using unique with both the 'stable' and 'rows' arguments with the ‘Results’ matrix may solve the problem. If not (since the individual rows may be unique even if there are non-unique values in each column), it will be necessary to decide what column should be used to determine ‘uniqueness’. The first occurrences of each value of that column (the ‘ia’ or second output of unique) can then be used to define the rows, although some rows will be missing. There may be no one correct approach to this, and it may require experimentation to see what works best.
.
7 Commenti
Più risposte (2)
dpb
il 27 Dic 2021
Error using griddedInterpolant
Sample points must be unique.
comes from
x = Results(:,1);
y = Results(:,2);
...
[X,Y]=meshgrid(x,y);
The x and y vectors must be unique for the MATLAB interpolation functions to work; your data has repeated points.
0 Commenti
Vedere anche
Categorie
Scopri di più su Formatting and Annotation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!