How to Interpolate scalar on new grid
조회 수: 7 (최근 30일)
이전 댓글 표시
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
dpb
2021년 12월 27일
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
2021년 12월 27일
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
추가 답변 (2개)
dpb
2021년 12월 27일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






