Plotting a 2D vector field with a 3D matrix quiver -MATLAB - matlab

I am having real trouble with vectors/matrices etc. in matlab.
I have a 3D vector nxmx2 where n is my x-coordinate, m is my y-coordinate and for each (x,y) there are two z values, the first being my velocity in the x-direction, and the second being my velocity in the y-direction.
How do I plot these velocity functions on a x-y graph? I have been trying with things like:
quiver(A)
quiver(A,A,A(:,:,1),A(:,:,2))
But can't seem to get it to work.

The correct way to use quiver for the input you describe is
quiver(A(:,:,1), A(:,:,2));
If you want the axes of the plot to have equal-spaced values different than 1,2,... you should use meshgrid to generate them, as shown in the examples in the quiver documentation.

Related

Embedding 2d plot into a 3d plot in matlab?

I have a set of data vectors z that has this 2d plot
How would I go about embed this set of data into a 3d plot like this in matlab? I'm asking for advice and suggestions. The theory I'm trying to employ is "for each data vector~zj, “copies” the data vector intothe first two entries of a 3D data vector~yjand then computes the squared length of~zj as the third entry of~yj. " or kernel trick.
Your 2d data will somehow be in the form, that you have x-coordinates and y-coordinates. Let's say you have a vector x and a vector y for simplification.
As you found out the plot3-function proivdes functionality to plot arbitrary points in 3d without the need of generating a mesh. What you need additionally is a third vector z with data for the 3rd dimension.
So what else can you do? The thing I am thinking about is rotating the plane you are plotting you "2d" data:
Rotational matrices can be seen here:
https://en.wikipedia.org/wiki/Rotation_matrix

matlab - plot inequality in 3d with surf

I want to plot an inequality in 3d using surf. My condition is
0<=x<=1
0<=y<=1
0<=z<=x/(1+y)
I can create a surface plot using the following commands
[x y]=meshgrid(0:0.01:1);
z=x./(1+y);
surf(x,y,z);
This plot gives me regions where z=x/(1+y) but I am interested in regions where 0<=z<=x/(1+y) over all values of x and y. However, I am unable to plot/color the region explicitly. Can you please help.
A similar question has been asked but there was no acceptable answer and my question is also different.
Using isosurface you can show the boundary. There are two options, first create the points
[X,Y,Z]=meshgrid(0:.01:1);
then plot the boundaries in the z-direction (i.e. Z=0 and Z=X./(1+Y))
isosurface(X,Y,Z,Z.*(X./(1+Y)-Z),0)
or plot all the boundaries (including X=0, X=1, Y=0 and Y=1)
isosurface(X,Y,Z,Z.*(X./(1+Y)-Z).*X.*(X-1).*Y.*(Y-1),0)
All you have to do is come up with a function that is constant on any boundary, its value inside or outside is irrelevant as long as it is not zero.

Matlab - Multiple 2D plots along a 3rd dimension

I'm trying to plot many 2D plots (x,y).
But...
each 2D plot is for a constant z.
So really my data is (x,y,z) but not z(x,y), which I believe are the requirements for using the "surf" command.
Could anyone help with this?
Example,
x = velocity
y = drag
I have multiple runs of y(x) for a constant temperature, z.
I just want to plot each (x,y) along a 3rd axis, temperature z.
Ideally I'd also want some sort of contour between the (x,y) plots so I can show the peaks/troughs etc.
Any help would be great.
If the runs are not independent (there is some trend over multiple runs) then it may make sense to use surf. You then need to construct your data such you have an X,Y, and Z - in this case I'd suggest you use the drag measurements as your Z (height).
Assuming that you have all the drag/velocity data in drag and velocity which are both of size [data points x number of runs]:
% construct matrix of run numbers
runs = repmat(1:numruns, [1, datapoints]);
runs = reshape(runs, datapoints, numruns);
% plot and label
surf(runs,velocity,drag);
xlabel('runs')
ylabel('velocity')
zlabel('drag')

How to plot 4D contour lines (XYZ-V) in MATLAB?

I have dataset of XYZ as the coordinates and V as the value at each point (100x4 matrix).
I plot the 3D surface using patch. (by faces & vertices)
How can I plot the contour lines of V (NOT Z) over the 3D surface !?
( The Contour3 function plots 3D contour lines of Z ; But I need contour lines of V. )
Actually I want something like this or this.
Thanks a billion for your help.
Well actually I found out that the isosurface command is exactly what I want.
However, this command requires the V data to be a 3D matrix. But my V is a vector. And the data in it is completely non-uniform and irregular. Now here rises a new question :
How can I convert this non-uniform vector to a 3D matrix, so that it's ready to be used with isosurface command !!?
Please help me with this.
cont3d from MathWorks FileExchange is not exactly what you're looking for, but it may give you some ideas.

plot a set of 3D data in different angles in MATLAB

I have a formula that depends on theta and phi (spherical coordinates 0<=theta<=2*pi and 0<=phi<=pi). By inserting each engle, I obtained a quantity. Now I have a set of data for different angles and I need to plot the surface. My data is a 180*360 matrix, so I am not sure if I can use SURF or MESH or PLOT3. The figure should be a surface that include all data and the axes should be in terms of the quantity, not the quantity versus the angles. How can I plot such a surface?
I see no reason why you cannot use mesh or surf to plot such data. Another option I tend to use is that of density plots. You basically display the dependent variable (quantity) as an image and include the independent variables (angles) along the axis, much like you would with the aforementioned 3D plotting functions. This can be done with imagesc.
Typically you would want your axes to be the dependent variables. Could you elaborate more on this point?
If I understand you correctly you have calculated a function f(theta,phi) and now you want to plot the surface containing all the points with the polar coordinated (r,theta,phi) where r=f(theta,phi).
If this is what you want to do, the 2D version of such a plot is included in MATLAB under the name polar. Unfortunately, as you pointed out, polar3 on MatlabCentral is not the generalization you are looking for.
I have been able to plot a sphere with the following code, using constant r=1. You can give it a try with your function:
phi1=0:1/(3*pi):pi; %# this would be your 180 points
theta1=-pi:1/(3*pi):pi; % your 360 points
r=ones(numel(theta1),numel(phi1));
[phi,theta]=meshgrid(phi1,theta1);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
tri=delaunay(x(:),y(:),z(:));
trisurf(tri,x,y,z);
From my tests it seems that delaunay also includes a lot of triangles which go through the volume of my sphere, so it seems this is not optimal. So maybe you can have a look at fill3 and construct the triangles it draws itself: as a first approximation, you could have the points [x(n,m) x(n+1,m) x(n,m+1)] combined into one triangle, and [x(n+1,m) x(n+1,m+1) x(n+1,m+1)] into another...?