## NAN from griddata for scatteredInterpolant

Asked by Matan Zakay

### Matan Zakay (view profile)

on 17 Jun 2019
Latest activity Commented on by John D'Errico

### John D'Errico (view profile)

on 18 Jun 2019
Accepted Answer by John D'Errico

### John D'Errico (view profile)

Hello,
I have a set of data (x,y,v) that i got from CFD analesys.
x and y vector not unique or sort in any way, but there are not single (x,y) that repet.
i want to interpolate my data to get lower resolution but i cant get it to work
F = scatteredInterpolant(x_c,y_c,z_c);
xi=linspace(min(min(x_c)),max(max(x_c)),100);
yi=linspace(min(min(y_c)),max(max(y_c)),100);
zi=F(xi,yi);
[XI, YI] = meshgrid(sort(xi), sort(xi));
ZI = griddata(xi, yi, zi, XI, YI);
ZI just return NAN

### Release

R2018a ### John D'Errico (view profile)

Answer by John D'Errico

### John D'Errico (view profile)

on 17 Jun 2019

Your problem is that you have no idea how to use those tools. There is no need to use griddata AFTER you used scatteredInterpolant! Here is your data. Scattered data, with some nasty stuff to interpolate on the edges, but still what appears to be a single valued relationship. There will be some areas where you get garbage.
F = scatteredInterpolant(x_c,y_c,z_c);
xi=linspace(min(min(x_c)),max(max(x_c)),100);
yi=linspace(min(min(y_c)),max(max(y_c)),100);
[Xg,Yg] = meshgrid(xi,yi);
Zg = F(Xg,Yg);
surf(Xg,Yg,Zg)
xlabel X
ylabel Y
zlabel Z
grid on
box on
hold on
plot3(x_c,y_c,z_c,'r.') As you see, it follows the data, and does so reasonably well. In some places, around the edges, if you look carefully at the plot, you will see what I would call interpolation artifacts, what I called garbage before. They are more difficult to eliminate.

Matan Zakay

### Matan Zakay (view profile)

on 17 Jun 2019
Thanks for the answer.
do you have another suggestion to get lower resolution?
John D'Errico

### John D'Errico (view profile)

on 18 Jun 2019
Lower resolution? What does that mean? I already showed you how to interpolate. Just use scatteredInterpolant, PROPERLY, as I did. The result will be an interpolated surface.
But LOWER resolution? Surely you mean higher resolution? Lower resolution would be the equivalent of a coarser set of points.
But if ytou insist on a LOWER resolution interpolant, just do this:
F = scatteredInterpolant(x_c,y_c,z_c);
xi=linspace(min(min(x_c)),max(max(x_c)),10);
yi=linspace(min(min(y_c)),max(max(y_c)),10);
[Xg,Yg] = meshgrid(xi,yi);
Zg = F(Xg,Yg);
surf(Xg,Yg,Zg)
So there a 10x10 grid. REALLY low rsolution. Why? God only knows.
If you meant higher resolution, the answer is still the same. Change your grid spacing to be more fine, perhaps 1000 points in each dimension. And since the default on scatteredInterpolant is a linear interpolant, you might change to the 'natural' interpolation method, which is C1 continuous.
For example, given a surface defined by 4 points on a unit square, we see:
xy = [0 0;0 1;1 0;1 1];
z = [0;0;0;1];
Flin = scatteredInterpolant(xy(:,1),xy(:,2),z,'linear');
Fnat = scatteredInterpolant(xy(:,1),xy(:,2),z,'natural');
xi=linspace(0,1,100);
yi=linspace(0,1,100);
[Xg,Yg] = meshgrid(xi,yi);
surf(Xg,Yg,Flin(Xg,Yg)) surf(Xg,Yg,Fnat(Xg,Yg)) I've rotated each surface around to point out the differences in the interpolation. The "natural" interpolant will be "smoother", in the sense that it is a C1 interpolant. But it will still show some interesting artifacts - artifacts that can sometimes be problematic depending on the application.