constrain surf() plot region

Hi all, I'm working with the surf(X,Y,Z) function, and trying to force some values to be specific. I'm basically trying to plot a 3D sin-wave, rotationally symmetric about (0,0) and contain a specific number of waves in any direction, then outside this region, force the Z values to zero. Here's code attempts using an 'if' but the Z=0 isn't applied at larger radii than Xmax:
NumberWaves = 2;
Each_Division = 0.1;
X_max = NumberWaves * (2 * pi) ;
X_min = -X_max;
Y_min = X_min;
Y_max = X_max;
[X,Y] = meshgrid(X_min : Each_Division : X_max);
Rad = sqrt (X.^2 + Y.^2 );
if (Rad < X_max)
Z=0;
else
Z = (((sin(Rad)) + 1) / 2);
end
figure;
colormap hsv;
surf(X,Y,Z, 'EdgeColor' ,'None');
xlabel('X (**)');ylabel('Y (**)');zlabel('Red Scale');
Alternatively to the forcing Z to a value, I was trying to find references to only plot the surface out to a given radius, which again would be rotationally symmetric. I'm very new to Matlab, so any help appreciated! Thanks!

 Respuesta aceptada

Kelly Kearney
Kelly Kearney el 10 de Nov. de 2014

0 votos

I would recommend setting up your grid in polar coordinates. The surf command (along with mesh, pcolor, etc.) doesn't require a rectilinear grid, so starting in terms of r and theta allows you to set the cutoff exactly without any setting-to-0 or NaN-masking:
NumberWaves = 2;
Each_Division = 0.1;
rmax = NumberWaves * (2 * pi) ;
r = linspace(0, rmax, 50);
theta = linspace(0, 2*pi, 100);
[r,theta] = meshgrid(r, theta);
z = (sin(r) + 1)./2;
x = r .* cos(theta);
y = r .* sin(theta);
surf(x,y,z);

5 comentarios

Pete
Pete el 10 de Nov. de 2014
Thanks Kelly, this did the job. Another question which I will post later after some browsing through help is to convert the Z scale colour to RGB, but only have the red channel used (so output would be scaled from 0 to 1 in red only (did try
colorplot([Z 0 0])
but ineffective.
Any advice?
Pete
Kelly Kearney
Kelly Kearney el 10 de Nov. de 2014
You mean you want a colorbar that goes from white to red over the range of 0-1?
cmap = flipud([ones(20,1) linspace(0,1,20)'*ones(1,2)]);
colormap(cmap);
set(gca, 'clim', [0 1]);
colorbar;
Pete
Pete el 11 de Nov. de 2014
Hi Kelly
Many thanks for this answer. This is almost exactly what I want, just that I want it going from black to red. Can you explain the ones function as used above - I'm trying to figure it out in terms of the [RGB] format, but I don't think I'm seeing the syntax properly (I am very new to matlab!)
Many thanks,
Pete
Pete
Pete el 11 de Nov. de 2014
OK, all sorted - changed the main line here to:
cmap = ([linspace(0,1,101)'*ones(1,1) linspace(0,0,101)'*zeros(1,2)])
and it's now all working. A handy little tool that will come in useful I'm sure! Many thanks Kelly for this.
Pete
Kelly Kearney
Kelly Kearney el 11 de Nov. de 2014
Just so you know, you've got some unnecessary calculations there. You can get the same with
cmap = [linspace(0,1,101)' zeros(101,2)];

Iniciar sesión para comentar.

Más respuestas (1)

Pete
Pete el 10 de Nov. de 2014

0 votos

Hi Star Strider,
Surely Xmax is my domain (square in XY), but if I look at +X+Y, the radius is > Xmax, so at this point I would expect the Z value to be forced to zero?
Is there a way to crop the surface? Looking at the figure, ideally I want to set my Z value to 0.5, but the value is arbitrary at this point - or can I crop the graph once 2 waves have been completed?

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Nov. de 2014

Comentada:

el 11 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by