Revolve a Plot around y axis to generate a 3D plot

I am interested in revolving a plot around the yaxis to generate a 3D plot.
Say,
theta =linspace(-pi/2,pi/2,100);
a=.75;
b=.25;
X=a*cos(theta);
Y=b*sin(theta)
Now I want to revolve that around the Y axis to generate a 3D plot. How would I do that without using the cylinder function?
I looked at some of the solutions on here, but I apply what they have I think its revolving around the x-axis instead of the y-axis.

Answers (2)

%2D Data
n = 100;
theta = linspace(-pi/2,pi/2,n);
a=.75;
b=.25;
X=a*cos(theta);
Y=b*sin(theta);
%plot the 2D curve
figure
plot(X, Y)
xlabel x
ylabel y
%% Generate data for x and z, as revolution is around y-axis
t0 = linspace(0, 2*pi, n).';
x = X.*cos(t0);
z = X.*sin(t0);
%repeat the same data for y
y = repmat(Y, n, 1);
%plot the surface
figure
surf(x, y, z)
xlabel x
ylabel y
zlabel z
%% Change the view to x-y plane to check
figure
surf(x, y, z)
xlabel x
ylabel y
zlabel z
view([0 0 1])

4 Comments

This doesn't look stretched out enough, I think it should look flatter like the one shown below. How could you modify to do that?
The axes have different scale factors.
Add
axis equal
to the figures.
@Torsten, Thanks.
@Kenneth, here is the modified result -
%2D Data
n = 100;
theta = linspace(-pi/2,pi/2,n);
a=.75;
b=.25;
X=a*cos(theta);
Y=b*sin(theta);
%plot the 2D curve
figure
plot(X, Y)
xlabel x
ylabel y
%% Generate data for x and z, as revolution is around y-axis
t0 = linspace(0, 2*pi, n).';
x = X.*cos(t0);
z = X.*sin(t0);
%repeat the same data for y
y = repmat(Y, n, 1);
%plot the surface
figure
surf(x, y, z)
xlabel x
ylabel y
zlabel z
axis equal
%% Change the view to x-y plane to check
figure
surf(x, y, z)
xlabel x
ylabel y
zlabel z
view([0 0 1])
axis equal

Sign in to comment.

The easiest way to do this is to start with the sphere function and then scale it.
Try this —
theta =linspace(-pi/2,pi/2,100);
a=.75;
b=.25;
X=a*cos(theta);
Y=b*sin(theta);
figure
plot(X, Y)
grid
xlabel('X')
ylabel('Y')
zlabel('Z')
axis('equal')
title('Elevation Profile')
[Xs,Ys,Zs] = sphere(50);
Xs = Xs*a;
Ys = Ys*a;
Zs = Zs*b;
figure
hs = surf(Xs, Ys, Zs);
xlabel('X')
ylabel('Y')
zlabel('Z')
axis('equal')
colormap(turbo)
% rotate(hs, [1 0 0], 90)
figure
hs = surf(Xs, Ys, Zs);
xlabel('X')
ylabel('Y')
zlabel('Z')
axis('equal')
colormap(turbo)
view(0,90)
title('Top View')
figure
hs = surf(Xs, Ys, Zs);
xlabel('X')
ylabel('Y')
zlabel('Z')
axis('equal')
colormap(turbo)
view(90,0)
title('Side View')
Make appropriate changes to get the result you want.
.

3 Comments

This looks correct. Is there anyway to do this without using the sphere function? I am not a fan of using built in functions just because I am never sure what they would give me in future matlab updates.
I want to use my X coordinates and Y coordinates that are updated throughout the loop to get this shape as it evolves over time
The sphere function has been stable for as long as I’ve been using it, literally decades. (There is no absolute guarantee that all code is going to be compatible with all future MATLAB releases, regardless.) With respect to evolving over time, that requires updating the variable multiplications:
[Xs,Ys,Zs] = sphere(50);
Xs = Xs*a;
Ys = Ys*a;
Zs = Zs*b;
with each update of ‘a’ and ‘b’.
If you want to animate it, that means either re-drawing it each time and displaying them, or storing all the updates in a matrix (most likely a cell array) and then looping through the matrix to display them. See the documentation section on Animation for those details.
.

Sign in to comment.

Products

Release

R2023a

Asked:

on 17 Nov 2023

Commented:

on 21 Nov 2023

Community Treasure Hunt

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

Start Hunting!