SLicing a 3-d plot in MATLAB - matlab

Suppose in MATLAB I have obtained a 3-D plot, like surf(peaks(20)) how do I get a slice along the plane X=0, and the corresponding 2-D plot?

You can make a contour plot:
data = peaks(50);
figure;
surf(data);
figure;
[C,h] = contour(data, [0 0]);
clabel(C,h);

Related

Scatter plot on top of a surface plot has garbled points

I have the following code that overlays a scatter plot on a surface.
Notice that some of the rounded points are chopped off.
figure
scatter(rand(20,1)*10,...
rand(20,1)*20,...
'o', 'LineWidth',5, ...
'MarkerFaceColor', 'black', ...
'MarkerEdgeColor', 'black')
hold on
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z)
view(2)
axis equal square;
Here is the output:
That's actually not a bug. It happens because you use surf which is a 3D plotting tool, and overlay it with small spheres at the height Z=0 generated by the scatter plot. Some of the spheres are cut by the 2d surface manifold at Z=0, some are below the manifold. Choosing view(2) show the part of the scatter plot that is only above the manifold that surf created (or that the surf Z values are <0) .
Change to view(3) and play with the camera angles to see it:
If you want to overlay a scatter plot on the surf data, you can, but you need to take a different path, here are a few suggestions:
add transparency to the surface plot, for example add alpha(0.5) after the surf line in your code and see...
add an artificial Z value to the surf plot so it will not intersect with the scatter plot at Z=0, for example edit the following line to : Z = sin(X) + cos(Y)-pi; ...
plot it differently (first using imagesc then scatter), for example:
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
imagesc(1:0.5:10,1:20,Z) % the same meshgrid values are used
hold on
scatter(rand(20,1)*10,...
rand(20,1)*20,...
'o', 'LineWidth',5, ...
'MarkerFaceColor', 'black', ...
'MarkerEdgeColor', 'black')
axis equal square;

How to plot a histogram as a scatter plot in matlab?

The y axis should be frequency (which is the histogram part) while the x axis is the xdata used to plot the histogram. Essentially what I need is exactly a histogram but instead of bars, there should be points.
I'd do that using the histcounts command. It's like the histogram command, but instead of plotting the data, it returns the number in each bin and the bin edges. You can then plot that as points using the general plot command.
x = randn(1,1000); %Generate some data to plot
figure(1); clf;
subplot(2,1,1); hold on;
h = histogram(x,'normalization','probability');
title('Plotted as a histogram');
ylabel('Frequency');
subplot(2,1,2); hold on;
[N, edges] = histcounts(x,'normalization','probability');
centers = (edges(1:end-1) + edges(2:end))./2; %histcounts gives the bin edges, but we want to plot the bin centers
plot(centers,N,'ko');
title('Plotted as points');
ylabel('Frequency');

how to fill colors between contours in matlab

I have plotted multiple contours in matlab with the hold on command, using the contour function of MatLab. How can I go ahead if I want to fill color between the first and the last contour. I tried contourf function but it didnt work out that way.
Thanks in advance.
I have written two simple lines which plots the zero level set contour after every iterations.
hold on;
contour(X,Y,phi,[0,0],'r');
This can be done by using the get command to get individual components from the graph. For example:
[x, y, z] = peaks; % Generate some data
figure; surf(x, y, z); % Show
figure;[c, h] = contourf(x, y, z, [0 0]); % Build and show contour plot
patches = get(h, 'children'); % Get different patches
set(patches(1), 'facecolor', 'r') % Colour one red

3D plot of part of hyperboloid

I would like to plot a 3D figure of a hyperbole that is cut down at the bottom of it in the following way (figure)
any ideas?
OK, here's my stab at your problem. This is the experimental script I've been using:
%%# first part
%#------------------
clf
%# use cylinder to get unit cone
[x,y,z] = cylinder( linspace(1, 0, 1e3), 1e3);
%# intersect the cone with this surface
inds = z < (cos(x).*sin(pi*y/2)+1)/4;
x(inds) = NaN; %# remove all corresponding
y(inds) = NaN; %# indices, in all arrays
z(inds) = NaN;
%# Now plot the cone. Note that edges are ugly when
%# using a large number of points
surf(x, y, z, 'edgecolor', 'none');
%%# second part
%#------------------
hold on
%# the surface to intersect the cone with
f = #(x,y) (cos(x).*sin(pi*y/2)+1)/4;
%# add the surfacfe to the cone plot
[x,y] = meshgrid( linspace(-1,1, 1e3) );
surf(x,y, f(x,y), 'edgecolor', 'none')
The first part shows a cone intersected with a curve. You might want to tinker a bit with the curve to get the overall shape right, which is what the second part is for.
If you want a paraboloid (or other), just use
[x,y] = meshgrid( linspace(-1,1, 1e3) );
z = 1-x.^2-y.^2; %# or whatever other equation
instead of the cylinder command.

Drawing the derivative of a curve

I have a 3D curve in MATLAB, now I want to draw the derivative of that curve in another graph?
For example, for y = x2 the derivative w.r.t. x is y = 2x.
How can I do this ?
I do not understand the '3D' part. Why is y=x^2 a 3D curve?
But if you want to plot y=x^2 and its derivative on the same plot, use ezplot
clear all; close all;
syms x
y=x^2;
h=ezplot(y,[-6,6]);
set(h, 'Color', 'r');
hold on;
ezplot(diff(y,x),[-6,6]);