MATLAB Answers

0

NAN from griddata for scatteredInterpolant

Matan Zakay さんによって質問されました 2019 年 6 月 17 日
最新アクティビティ John D'Errico
さんによって コメントされました 2019 年 6 月 18 日
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

  0 件のコメント

サインイン to comment.

製品


リリース

R2018a

1 件の回答

John D'Errico
回答者: John D'Errico
2019 年 6 月 17 日
 採用された回答

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.
untitled.jpg
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.')
untitled.jpg
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.

  2 件のコメント

Matan Zakay 2019 年 6 月 17 日
Thanks for the answer.
do you have another suggestion to get lower resolution?
John D'Errico
2019 年 6 月 18 日
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))
untitled.jpg
surf(Xg,Yg,Fnat(Xg,Yg))
untitled.jpg
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.

サインイン to comment.



Translated by