Mesh function seems to swap x and y values - matlab

I am trying to plot a very simple function in the 3d plane.
f=zeros(101,101);
xs=0:0.1:10;
ys=0:0.1:10;
for j=1:101
f(1,j)=ys(j);
end
Here are 3 plots:
The first is a plot of f vs x at ymin:
figure; plot(xs,f(:,1),'*r')
xlabel('x')
ylabel('f')
The second is a plot of f vs y at xmin:
figure; plot(ys,f(1,:),'*r')
xlabel('y')
ylabel('f')
And finally the third is a 3d mesh:
figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')
However the mesh plot seems to contradict the 2 2d plots, it seems to have the x and y switched around if you get me. Can anyone help? Should it be mesh(ys,xs,f) for some reason? Thanks!

mesh didn't switch your x and y, it's a matter of definition. Don't forget that a matrix doesn't have an x axis or a y axis but rather a row dimension and a column dimension. The row dimension is normally stated first in a pair just like the x dimension but if you are equating it too an image then you would normally have the x axis going across which is actually along the column axis!
Try like this rather
for j=1:101
f(j,1)=ys(j);
end
figure; plot(xs,f(1,:),'*r')
xlabel('x')
ylabel('f')
figure; plot(ys,f(:,1),'*r')
xlabel('y')
ylabel('f')
figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')

Related

How do I create a colormap from an existing data set in Matlab?

I currently have a 2-D contour plot in Matlab from an existing data set. I made an [x,y] mesh grid and used this mesh grid and z-data to produce a 2-d contour plot using contourf(x, y, z). My goal is to reproduce this same data as a colormap, with smooth color gradients, rather than as a 2-d contour plot, with distinct color bands.
I have tried using imagesc(x, y, z) with [x,y] as a mesh grid and without. I ended up with an error function "Attempt to execute SCRIPT imagesc as a function:"
x = 0.1:0.1:1
y = 0.1:0.1:1
[X, Y] = meshgrid( x , y )
Z = #data#
contourf( X , Y , Z )
title
xlabel
ylabel
I'm not quite sure what's going wrong with your attempt to use imagesc... When I used your x and y and defined Z=sin(X*20)+sin(Y*20); and ran imagesc(x,y,Z) I got
Looking at the error message you're getting I suspect that you have a script somewhere saved as imagesc which is somehow overwriting the imagesc function. Try running edit imagesc and see what comes up, is it a function?
Now as far as making this smooth looking you have two options. Firstly you could just use a higher density of points as opposed to a 10x10 grid. For example
x = linspace(0,1,1000);
y = linspace(0,1,1000);
[X, Y] = meshgrid( x , y );
Z=sin(X*20)+sin(Y*20);
imagesc(x,y,Z)
gives
Alternatively, if you want/need to stick with the low density of points you can use pcolor(X,Y,Z) and then set shading interp which gives

Defining surfaces not just in terms of z

I have three different surfaces and I want to display all of them in one figure.
The problem is, that I have one surface defined in terms of z (means that I got x and y values and a grid which specifies the z-values for each combination) and two other ones which are defined in terms of x. This means that there exist various z-values for one x,y-pair.
My idea was:
figure
surf(x,y,zgrid)
hold on
surf(x,ygrid,z)
surf(x,ygrid2,z)
hold off
I hoped MATLAB would manage it by itself but it doesn't.
Do you have any ideas how to get the wanted results? I want to display all of them in one plot to show the cross-sections.
Here is an image of how it should more or less look like:
If there is a more beautiful method to display this, please let me know.
You didn't specify what exactly was going wrong, but I'll hazard an educated guess that you got an error like the following when you tried to plot your second surface:
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
I'm guessing your x, y, and z variables are vectors, instead of matrices. The surf function allows for the X and Y inputs to be vectors, which it then expands into matrices using meshgrid. It doesn't do this for the Z input, though.
It's best practice, in my opinion, to just use matrices for all your inputs anyway. Here's an example where I do this (using meshgrid) to plot three surfaces of a cube:
% Top surface (in z plane):
[x, y] = meshgrid(1:10, 1:10);
surf(x, y, 10.*ones(10), 'FaceColor', 'r');
hold on;
% Front surface (in y plane):
[x, z] = meshgrid(1:10, 1:10);
surf(x, ones(10), z, 'FaceColor', 'b');
% Side surface (in x plane):
[y, z] = meshgrid(1:10, 1:10);
surf(ones(10), y, z, 'FaceColor', 'g');
axis equal
And here's the plot:

plot 2D intensity figure in 3D coordinate system

I have a 2D intensity plot, as in this example:
[xx yy] = meshgrid(0:0.1:1, 0:0.1:1);
figure(1)
imagesc(sin(xx)) %(x,y)-plot at z=0
Now, as I have noted in the comment, this plot is in the xy-plane and I have taken z=0. I'd like to plot this in a 3D coordinate system as a function of x and y, but taking z=0. I tried with plot3 in this way, plot3(xx,yy,cos(yy)), but that only plots lines and gives them a curvature which I am not interested in. I'd like only a plane at z=0.
You can use surf to accomplish this. We use the xx and yy meshgrid outputs as the x and y positisions, a matrix of zeros as the z value and sin(xx) as the color. We also remove the edges by setting the EdgeColor to none.
surf(xx, yy, zeros(size(xx)), sin(xx), 'EdgeColor', 'none')

How can I plot a 3d vector fast in matlab?

I'm working on a project which includes use of accelerometer and gyroscope to get the orientation an object. I can pass the 3d orientation vector from arduino to matlab via serial communication.
I want to plot the vector in matlab to make real time analysis. I'm using quiver3d and drawnow functions in a loop to plot the vector but quiver3d function is very slow so I can see the orientation of object after like 20 seconds later.
Is there any way to plot 3d vectors faster?
Thanks.
quiver plot may be too much for plotting only one vector in 3-D. You can achieve a similar plot by using a simple plot3 such as the one plotted below.
In this plot, the origin of the vector is the blue dot, and the direction is given by the red line.
The code
%v is the direction of the vector (3 cartesian coordinates)
v = sort(randn(100,3));
v = bsxfun(#rdivide,v,sqrt(sum(v.^2,2)));
%xyz the origin of the vector
ind = linspace(-pi,pi,100);
x = cos(ind);
y = sin(ind);
z = ind;
%the plotting function
figure
for ii = 1:numel(ind)
plot3(x(ii),y(ii),z(ii),'bo'); %origin in blue
set(gca,'XLim', [-3 3], 'YLim', [-3 3], 'ZLim', [-3 3]);
hold on;
hl = plot3( linspace(x(ii), x(ii)+v(ii,1),10), ...
linspace(y(ii), y(ii)+v(ii,2),10), ...
linspace(z(ii), z(ii)+v(ii,3),10), ...
'r'); %direction in red
view(80,10);
pause(0.1);
%clf
end

plotting trajectory data in matlab

I have trajectory information in 3 dimensions in matlab. These are of a gesture someone is making. When I connect the points in matlab by using plot3, I can see the trajectory nicely.
However, the trajectory is a line in the plot, but I don't know in which direction the gesture has been made as the time is not visualized. Is it possible to visualize this in a 3d plot (where the dimensions are x, y and z)? For example, the colour at the start is bright red and the colour at the end is black.
Thanks for your help,
Héctor
You need the comet3 plot (if you don't mind animations).
If you do mind animations, and you're looking for a static figure, I'd use a quiver.
Example:
% value of the parameter in the parametric equation
t = 0:0.5:2*pi;
% modified coordinate axes
u = [1 0 0].';
v = [0 2 0].';
% coordinates of the ellipse
Ell = bsxfun(#plus, bsxfun(#times, u, cos(t)), bsxfun(#times, v, sin(t)));
% difference vectors between all data points will be used as "velocities"
dEll = diff(Ell, 1,2);
% Quiver the ellipse
quiver3(...
Ell(1,1:end-1), Ell(2,1:end-1), Ell(3,1:end-1), ...
dEll(1,:), dEll(2,:), dEll(3,:), ...
2, 'r') % = scale, LineSpec
axis tight equal
Result: