Using griddata on an unstructured grid

Hello
This is probably a very silly question but I can't really wrap my head around griddata. I've used it previously but it was a lot more straight forward then!
So right now I have one set of coordinates (x,y) which represent and unstructured grid. I also have another set of coordinates with represent the centre of each of those triangles in the unstructured grid. I have a velocity value for each of the points within the triangle. What I need to do is to interpolate the data from the centre of the triangle to the different node points, which would be my first set of coordinates. I've been trying to use griddata but keep getting error messages.
I'm not sure if I've explained myself very well but I've attached my code to this question so hopefully that will be clearer.

 Réponse acceptée

KSSV
KSSV le 6 Sep 2016
Modifié(e) : Stephen23 le 6 Sep 2016

0 votes

As your data is unstructured, you can consider this data as scattered data. Why don't you use scatteredInterpolant? I have used this quite lot of times for unstructured grid and it worked perfectly for me.
More on:

10 commentaires

Meghan
Meghan le 6 Sep 2016
Hi Siva
I had considered using scatteredInterpolant, but when I tried it gave me an error that my sample values must be a double array. I've never had to try to change an array from single to double so I'm not really too sure how to do it. And googling the answer didn't really help me much. Do you have any suggestions?
KSSV
KSSV le 6 Sep 2016
Can you attach your data?
"I've never had to try to change an array from single to double so I'm not really too sure how to do it."
doubleData = double(singleData)
Meghan
Meghan le 6 Sep 2016
I'm actually not able to.
But OSRef_nodeX & OSRef_nodeY are 46878x1 double matrices, while u is a 79244x1 single matrix.
I know this makes everything a lot more complicated that I can't share it, so I understand if you can't help.
Thank you :)
Thank you Stephen, that did it! However now I'm getting the error:
Error using scatteredInterpolant
The number of data point locations should equal the number of data point values.
KSSV
KSSV le 6 Sep 2016
Your x,y, and z values shall be of same dimension.....
Meghan
Meghan le 6 Sep 2016
Modifié(e) : Meghan le 6 Sep 2016
But they're not. This is why I need to interpolate. I have 46878 nodal points. This gives me 79244 points in the centre of the triangles because I have more triangles than nodes. What I need to do is interpolate the velocity data from the centre of the triangle (79244 points) to the nodes of the triangle (46878 points).
Right now I have
% Interpolating from the triangle to the nodes
XY=longlat2os(mesh.uvnode(:,1),mesh.uvnode(:,2),'degrees','northeast'); %coordinates for all trinodes
u1=A.FVCOM1.u(1,1,:); u=squeeze(double(a));
XY1=griddata(OSRef_nodeX,OSRef_nodeY,u,XY(:,1),XY(:,2),'linear');
Which looks like XY1=griddata(46878x1 double, 46878x1 double, 79244x1 single, 79244x1 double, 79244x1 double, 'linear');
And I'm getting the error:
Error using griddata (line 109)
X and Y must be same length as Z or the lengths of X and Y must match the size of Z.
So I'm wondering if what I want is even possible?
Steven Lord
Steven Lord le 6 Sep 2016
Construct your scatteredInterpolant using the coordinates of the centers of the triangles and the value at each of those centers of the variable you want to interpolate. Then evaluate the scatteredInterpolant using the coordinates of the vertices of the triangles.
In the 2-D Interpolation example on the documentation page I linked above, x and y will be the coordinates of the triangle centers. v will be the values of the data you're trying to interpolate at each of those centers. xq and yq will be the coordinates of the triangle vertices. vq will be the interpolated values of the data at each of those vertices. While this example uses mesh and plot3 to plot the data, you may find trisurf or trimesh (with the interpolated vq values serving as the Z coordinates of each of the points) better suited to display your triangular "grid".
Meghan
Meghan le 6 Sep 2016
You are a life saver! I've been trying to figure this out all day!
Thanks so much :)
Steven I wonder if you can help me. That method above does what I want when the v value is 79244x1. Can I use the same method if v is 79244x24x10? I've been fiddling with it this morning and haven't had much luck. I keep getting the error
Error using scatteredInterpolant
Input data point values have invalid dimension. The data must be specified in column-vector format.
I've introduced a z matrix so that the input is
XY3=scatteredInterpolant(uvX,uvY,mesh.uvdepth,u3,'linear');
Not sure if that is correct? Any advice?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by