Creating a grid from a 2D scatter plot of vectors

So I have a list of x positions, y positions, x velocity and y velocity, I want to turn this in to 4 grid matrices with (i,j) from each of the 4 matrices corresponding to the values for x,y,u,v from the list above with the x, y values being in order in terms of their physical position. I have tried to just reshape the matrices but this isnt working and tried griddata but as I have two values for the velocity it wont work as v needs to be just a single value, and the velocity values are essentially random, so i can't create a fucntion that defines them over the domain, and if I get the resultant velocity vector this only defines the magnitude and not the direction.
I need to do this so I can do partial differentiation using the finite difference scheme. Does anyone have any suggestions as to how I might do this?

5 Comments

Attach your data. Along with code you have tried.
Why exactly does v have 2 values?
I have values from a velocity field, so the velocity at each point in x and y is defined by the x velocity and the y velocity.
yeah, so that's u and v, each with 1 value...
Sorry so I think I was a bit unclear there. I was trying to use this fucntion vq = griddata(x,y,v,xq,yq). where v = f(x,y). When I say my V has two values I mean that the values that I am trying to define along each x and y position are velcoities say, ux and uy, instead of u and v to avoid confusion. In this fucntion though v can only have one value, and I cant define my velocity only in terms of its magnitude as I los the information about the direction of the veloicty vector.
Maybe I should use a different method than griddata, I just need to create 4 matrices where the (i, j) value from each matrix corresponds to eachother in the velcoity field. I just wanted the x and y values in the matrix to corrspond to their positions reative to each other so that I can make a for loop which calculates du/dx, dv/dy etc for each vector using finite difference scheme, I know there are functions to do partial differentiation in matlab but it seems that they are based on differentiatng a function and not discrete values. The velocity values are essentially random as they are velocity vectors obtained using PIV.

Sign in to comment.

 Accepted Answer

Let x, y, xvel, yvel be your data.
m = 100 ; n = 100;
x = linspace(min(x),max(x),m) ;
y = linspace(min(y),max(y),n) ;
[X,Y] = meshgrid(x,y) ;
u = griddata(x,y,xvel,X,Y) ;
v = griddata(x,y,yvel,X,Y) ;
quiver(X,Y,u,v)

5 Comments

Thank you! Does quiver create a matrix from the list of vectors or is it just a plot? I actually just used meshgrid twice once for the x velocity and once for the y velocity and now I think I have the 4 matrices for FDS.
%Imports vector field
T = readmatrix('VectorField.txt');
%Removes last column of vector as it is not applicable
R = T(:,1:4);
%Removes NaN values
R2 = rmmissing(R);
%seperates each vector column in to x,y, vx, vy
xq = R2(:,1);
yq = R2(:,2);
u1= R2(:,3);
v1= R2(:,4);
spacing = xq(2,1)-xq(1,1);
X = min(xq):spacing:max(xq);
Y = min(yq):spacing:max(yq);
[x,y] = meshgrid(X,Y);
%Creates Meshgrid for x, y and u and v values
vv1 = griddata(xq, yq, u1, x,y );
vv2 = griddata(xq, yq, v1, x,y );
quiver is just a plot. It takes the data and plots the velocities in arrows.
Yes I thought so, thank you!
Thanks is accepting/ voting the answer. :)
Sorry will do that now, my first time asking a question so I didn't know!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!