How to Interpolate scalar on new grid

7 views (last 30 days)
MRobert on 27 Dec 2021
Commented: Star Strider on 28 Dec 2021
Dear forum,
I have a Results.csv file which contains several data from a numerical simulation (3 columns with 200 rows):
1. Column 1: X coordinate of the mesh/grid point;
2. Column 2: Y coordinate of the mesh/grid point;
3. 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?
Regards,
Robert
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);
dpb on 27 Dec 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.

Star Strider on 27 Dec 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.
Alternatively, the griddata function may sbe the best option here.
.
Star Strider on 28 Dec 2021
As always, my pleasure!
.

dpb on 27 Dec 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.

MRobert on 27 Dec 2021
Edited: MRobert on 27 Dec 2021
Thank you both for the interest and answers. When running:
x = Results(:,1);
y = Results(:,2);
T = Results(:,3);
[X,Y]=meshgrid(x,y);
Temp = griddata(x,y,T,X,Y,'linear')
I get the following warning:
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
> In griddata>useScatteredInterp (line 181)
In griddata (line 122)
In Test (line 24) --this line 24 is "Temp = griddata(x,y,T,X,Y,'linear')"
I don't really understand what "duplicate data points" refers to. When i create the grid some of data repeats, for example among Y axis there are different values of X for the same Y value. Even in this example when the grid of X and Y is created there are some duplicated values but when I run it I have no warning. I tried to solve it using "uniques" but it deletes way too many points and I also get array of different number of values for X and Y and I can't create the grid after.
I also attach a figure which should explain better the result I am looking for. Fig 1 shows the nodes from a coarse grid. In Fig 2 I put the scalar (its values are extracted in the nodes shown in Fig 1). Now I made a finner grid in another program, imported the coordinates in MatLab and I have the grid from Fig 3. The final results I am seeking is to interpolate Temp on this grid from Fig 3.
Thank you again,
Regards,
Robert
L.E. : If I remove view(2) from Fig. 2 and rotate the graph, the result looks like below

R2021b

Community Treasure Hunt

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

Start Hunting!