David_G
David_G

Reputation: 1157

Griddata has unwanted interpolation in Matlab

I'm trying to interpolate data from one grid to another in MATLAB. The first grid is unevenly spaced in y, while the second is not. When I perform the interpolation (using griddata), the result extrapolates across areas where data does not exist.

Here is the code, and plots of before and after the interpolation.

The input variables:

Xin2 = [619000      619000      619000      619000      619000      619000
  621000      621000      621000      621000      621000      621000
  623000      623000      623000      623000      623000      623000
  625000      625000      625000      625000      625000      625000
  627000      627000      627000      627000      627000      627000
  629000      629000      629000      629000      629000      629000
  631000      631000      631000      631000      631000      631000
  633000      633000      633000      633000      633000      633000
  635000      635000      635000      635000      635000      635000
  637000      637000      637000      637000      637000      637000
  639000      639000      639000      639000      639000      639000
  641000      641000      641000      641000      641000      641000
  643000      643000      643000      643000      643000      643000
  645000      645000      645000      645000      645000      645000
  647000      647000      647000      647000      647000      647000
  649000      649000      649000      649000      649000      649000
  651000      651000      651000      651000      651000      651000
  653000      653000      653000      653000      653000      653000
  655000      655000      655000      655000      655000      655000
  657000      657000      657000      657000      657000      657000
  659000      659000      659000      659000      659000      659000];

Yin2 = [-688.38      -626.71      -568.39       -514.5      -465.64      -422.03
  -688.36      -626.64      -568.28      -514.35      -465.46      -421.82
  -688.32      -626.51      -568.07      -514.06       -465.1       -421.4
  -688.25      -626.31      -567.74      -513.61      -464.54      -420.75
   -688.2      -626.18      -567.53      -513.33      -464.19      -420.34
  -688.19      -626.13      -567.45      -513.23      -464.06      -420.19
  -688.14      -625.99      -567.22      -512.91      -463.67      -419.73
  -688.06      -625.74      -566.82      -512.37      -463.01      -418.95
  -688.01       -625.6      -566.59      -512.06      -462.61      -418.49
     -688      -625.57      -566.54      -511.99      -462.53       -418.4
  -687.13      -623.02       -562.4      -506.38      -455.59      -410.27
  -677.94       -595.9      -518.32      -446.64      -381.64      -323.64
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3
  -677.06      -593.29      -514.08      -440.89      -374.52       -315.3];

Tempin2 = [0.74399        0.734      0.71456      0.68519      0.64358      0.58546
   0.7444      0.73442      0.71505      0.68578      0.64421      0.58598
   0.7446      0.73456      0.71517      0.68589      0.64428      0.58594
  0.74389       0.7338      0.71437      0.68505      0.64341      0.58507
  0.74176      0.73172      0.71234      0.68311      0.64166      0.58365
  0.73842      0.72847      0.70929      0.68033      0.63925      0.58175
  0.73554      0.72561      0.70641      0.67755      0.63666      0.57929
  0.73602      0.72568      0.70602      0.67671      0.63529      0.57718
  0.74297      0.73128       0.7101      0.67944      0.63682      0.57729
  0.75596      0.74156      0.71799      0.68532       0.6408      0.57963
  0.77081      0.75286      0.72441      0.68681      0.63783      0.57257
  0.77558      0.74655       0.7048      0.65198      0.58381      0.49389
  0.76412      0.73625      0.69626       0.6459      0.57968      0.49109
   0.7519      0.72508      0.68915      0.64346      0.58165      0.49733
  0.74398      0.72001      0.68661      0.64361      0.58471      0.50343
   0.7387        0.718      0.68708      0.64565      0.58851      0.50948
  0.73546      0.71749      0.68821      0.64767      0.59183      0.51471
  0.73255      0.71626      0.68831      0.64861      0.59397      0.51842
  0.72843      0.71274      0.68603      0.64778       0.5947      0.52053
  0.72166       0.7065      0.68148      0.64538      0.59429      0.52148
  0.71285      0.69917      0.67631      0.64259      0.59351      0.52205];

%And the code to make the interpolation variables and run the interpolation:

[X_i,Y_i]=meshgrid([Xin2(:,1)],[-700:5:-315]);
Temp_i=griddata(Xin2,Yin2,Tempin2,X_i,Y_i);

figure,pcolor(Xin2,Yin2,Tempin2), shading flat % original data
figure,pcolor(X_i,Y_i,Temp_i),shading flat %interpolated data

Before:

Before interpolation

After: After interpolation

Can anyone explain the dark blue triangle of extrapolated data (that joins diagonally from approximately (6.4e5,-325) to (6.2e5,-425), and how I can stop this?

NB I forgot to maintain the same caxis for both plots; This doesn't matter for illustrating where the unwanted interpolation occurs.

Upvotes: 1

Views: 1475

Answers (1)

Suever
Suever

Reputation: 65460

This happens because griddata by definition will not extrapolate, but the interpolation is based roughly upon the convex hull of your data. If you look at the input values for which you have data, the convex hull looks similar to the result shown in your second image.

This is the same as using the none extrapolation method for scatteredInterpolant.

No extrapolation. Any queries outside the convex hull of F.Points return NaN.

Here I have shown the convex hull in black (convhull) of your inputs (Xin2 and Yin2) super-imposed on your interpolation result.

K = convhull(Xin2, Yin2);
plot(Xin2(K), Yin2(K))

enter image description here

As far as how to prevent this, you could only query at points which you know are to be within the boundaries of your initial dataset. As far as how to do that, you'll want to create some outline of your initial data and use inpolygon to test each query point and then only pass the query points (X_i and Y_i) that are inside of this outline to griddata.

To get this outline you could use something like ordered_outline from the gptoolbox.

Upvotes: 2

Related Questions