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

조회 수: 8 (최근 30일)
Kenneth
Kenneth 2023년 11월 17일
댓글: Dyuman Joshi 2023년 11월 21일
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
Dyuman Joshi
Dyuman Joshi 2023년 11월 21일
Any updates, @Kenneth?

댓글을 달려면 로그인하십시오.


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