Using 3D plot to model orbit.

조회 수: 9 (최근 30일)
Anders Goetz
Anders Goetz 2020년 5월 7일
답변: Yash 2025년 7월 21일
Having problems with getting this right. Want to have the straight line equation move up the middle of the rising eliptical shape. If you run the code now the line goes nowhere near up the middle of the elipse. Just trying to code a basic orbit around a planet and if anyone has any suggestions to make this a little better that would be great!
Got this idea from MATLAB youtube video about 3D plots and wanted to explore it a little more.
t = linspace(0, 8*pi, 100);
x = 6*cos(t);
y = 4*sin(t);
z = t;
z2 = t/3;
x2 = t;
y2 = t;
for k=1:length(t)
clf
t_k = t(k);
x_k = x(k);
y_k = y(k);
z_k = z(k);
x2_k = x2(k);
y2_k = y2(k);
z2_k = z2(k);
plot3(x_k, y_k, z_k, 'bo', 'LineWidth', 2, 'MarkerSize', 16)
hold on
plot3(x, y, z, 'b-', 'LineWidth', 2)
hold on
plot3(x2_k, y2_k, z2_k, 'bo', 'LineWidth', 2, 'MarkerSize', 16)
hold on
plot3(x2, y2, z2, 'b-', 'LineWidth', 2)
grid on
xlabel('x')
ylabel('y')
zlabel('z')
title('Planetary Orbit')
drawnow
end

답변 (1개)

Yash
Yash 2025년 7월 21일
The straight line modeled in your code is a line going diagonally through space and not through the center of the ellipse. The spiral rises along the z-axis as 't' increases. You can replace the definition of the line with:
x2 = zeros(size(t));
y2 = zeros(size(t));
z2 = t;
This will plot a straight line along the z-axis, up the center of the ellipse. Below is the updated code:
t = linspace(0, 8*pi, 100);
x = 6*cos(t);
y = 4*sin(t);
z = t;
x2 = zeros(size(t));
y2 = zeros(size(t));
z2 = t;
for k=1:length(t)
clf
t_k = t(k);
x_k = x(k);
y_k = y(k);
z_k = z(k);
x2_k = x2(k);
y2_k = y2(k);
z2_k = z2(k);
plot3(x_k, y_k, z_k, 'bo', 'LineWidth', 2, 'MarkerSize', 16)
hold on
plot3(x, y, z, 'b-', 'LineWidth', 2)
hold on
plot3(x2_k, y2_k, z2_k, 'ro', 'LineWidth', 2, 'MarkerSize', 16)
hold on
plot3(x2, y2, z2, 'r-', 'LineWidth', 2)
grid on
xlabel('x')
ylabel('y')
zlabel('z')
title('Planetary Orbit')
axis equal
drawnow
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by