Interpolation of a 3D vector field in MATLAB - matlab

I'm trying to interpolate a 3D vector field. For every (x,y,z) position we have a vector (u,v,w). I have another set of points let's call them (xq,yq,zq) in which I don't have vector information (uq,vq,wq). I would like to interpolate my data to find the vectors at the points (xq,yq,zq).
I've tried to interpolate using several functions like griddata, by interpolating each vector component individually.
uq = griddata(x,y,z,u,xq,yq,zq);
vq = griddata(x,y,z,v,xq,yq,zq);
wq = griddata(x,y,z,w,xq,yq,zq);
I expect to obtain the vectors at the locations I specified, but the message I get is:
"Warning: The underlying triangulation is empty - the points may be
coplanar or collinear."
Is there a better way to interpolate a vector field?

Related

Plotting with a matrix in Matlab

The thing is i have a matrix and when i use imagesc() it goes like this but my goal is this.
So my question is does any one know which plot is this or some one has document about this, thanks.
If you have two vectors r and theta that give the polar coordinates, or two matrices rGrid and thetaGrid that give the polar coordinates for each element of the data matrix, then code like this will work:
r=linspace(1,20,20);
theta=linspace(0,2*pi,20);
data = r'.*sin(2.*theta); % INSERT DATA HERE
[thetaGrid,rGrid]=meshgrid(theta,r); % Create coordinate grid if needed
[xGrid,yGrid]=pol2cart(thetaGrid,rGrid);
surf(xGrid,yGrid,data); % Plot data
view(2);
Just keep in mind that the rows of the data matrix need to correspond to different radii, and columns need to correspond to different values of theta. If it's flipped, then transpose the matrix before plotting:
data = data';
Also, if the data doesn't wrap around from 0 to 2*pi radians, then repeat the first value of theta as the last value, and repeat the first column of the data matrix as a new final column:
theta(end+1)=theta(1);
data=cat(2,data,data(:,1));
There is also a 3D Polar Plot function on the MATLAB file exchange, but I do not have any experience using it: 3D Polar Plot

Interpolating scattered 3D electric field data

I have measured electric field data in three dimensions of the following form:
pos = [x1 y1 z1
x2 y2 z2
. . .
x1000 y1000 z1000]
ef = [e_x1 e_y1 e_z1
e_x2 e_y2 e_z2
. . .
e_x1000 e_y1000 e_z1000]
The positions are located within a half sphere. I want to be able to interpolate the electric field at some point of the sphere, so that I receive all the three values of the electric field components, not just the norm of the whole field. interp3 won't work as the points are not in a grid. scatteredInterpolant needs the norm as the input, and the generated function only returns the norm. Any suggestions on what function to use or how to solve this problem?
scatteredInterpolant only interpolates one column of samples at a time, but one way to interpolate a vector field is to interpolate each component of the vector independently.
This is straightforward with scatteredInterpolant.
componentInterpolants = cellfun(#(v) {scatteredInterpolant(pos,v)}, num2cell(ef,1));
This leaves you with a cell array of three scatteredInterpolant objects, one for each cartesian component of the field. By evaluating each of them individually for the specified coordinates and concatenating the result you get an electric field vector for each given coordinates. You could define a convenience function to give you the interpolated vector field in full for a given set off coordinates:
efInterpolant = #(coords) cell2mat(cellfun(#(interpolant) {interpolant(coords)}, componentInterpolants));
Verify that this interpolates the original vector field exactly (barring floating point error):
assert(all(all(abs(ef - efInterpolant(pos)) < eps * 2)));
If interpolating each cartesian component independently doesn't maintain some relationship between the vector components you expect within this field then you might need a different numerical method – perhaps some coordinate transformation before taking the same interpolation approach – but that's probably more a question for the Mathematics Stack Exchange site.

MATLAB integration of vector field

Say I have a discrete vector field u(x,y) and v(x,y). I have another scalar field vort(x,y). x and y are a meshgrid style set of coordinates. I want to set a contour level of my scalar vort, and integrate the vector field around that closed contour. How can I do this when I have discrete data, not a function?
contour(x,y,vort,[0.5 0.5]); %for example
I can extract from this the data points at all locations on the contour, but how do I integrate the vector field onto this curve?
I sorted this by:
use contourc to find the coordinates of the points in the loop
use improfile to interpolate to find the u and v values at a specified number of points around the loop
Find the angle (a) of the loop at each point
Integrate u * cos(a) - v * sin(a) using trapz

Sampling internediate points from x-y discrete mapping itself in Matlab

I have plotted a piece-wise defined continuous linear function comprising of several oblique straight lines joined end-to-end:-
x=[0,1/4,1/2,3/4,1];
oo=[1.23 2.31 1.34 5.69 7] % edit
y=[oo(1),oo(2),oo(3),oo(4),oo(5)];
plot(x,y,'g--')
I now wish to sample points from this plot itself, say i want the y corresponding to x=0.89. How to achieve that using Matlab? Is there a special function in-built in Matlab?
Yes, there's a built-in function for that: interp1:
vq = interp1(x,v,xq) returns interpolated values of a 1-D function at specific query points using linear interpolation. Vector x contains the sample points, and v contains the corresponding values, v(x). Vector xq contains the coordinates of the query points.
[...]
See the linked documentation for further options. For example, you can specify the interpolation method (default is linear), or whether you want to extrapolate (i.e. allow for xq values to lie outside the original x range).

How to create a 2D-matrix out of my data for surf()?

I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.