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

9 次查看(过去 30 天)
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.

回答(2 个)

Dyuman Joshi
Dyuman Joshi 2023-11-17
%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 个评论
Dyuman Joshi
Dyuman Joshi 2023-11-17
@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

请先登录,再进行评论。


Star Strider
Star Strider 2023-11-17
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 个评论
Kenneth
Kenneth 2023-11-17
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
Star Strider
Star Strider 2023-11-17
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.
.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by