Effacer les filtres
Effacer les filtres

3D그래프에서 함수를 점의 자취로 표현하려면 어떻게 해야 하나요?

2 vues (au cours des 30 derniers jours)
종은 정
종은 정 le 16 Nov 2021
Modifié(e) : Nivedita le 3 Mai 2024
3차원 상에서 공이 날아가는 걸 표현하고 싶어서, 점의 자취를 표현하는 식으로 하려고 합니다.
어떤 함수에 대해서 그래프로 표현할 때, 전체적인 그래프를 한 번에 보여주는 것이 아니라 변화하는 과정을 보여주고 싶습니다.

Réponses (1)

Nivedita
Nivedita le 3 Mai 2024
Modifié(e) : Nivedita le 3 Mai 2024
안녕하세요.
문의하신 내용은 영어로 답변해 드리겠습니다.
To represent the trajectory of a point (for example, a ball flying through the air) in 3D space using MATLAB, you can use a combination of functions like plot3, comet3, or even animate the path using a loop and the drawnow function.
1. Using "plot3" for Static Display: If you just want to plot the trajectory statically (all at once), you can use the "plot3" function. Assume you have equations or data for the (x), (y), and (z) coordinates over time.
% Example trajectory data
t = linspace(0, 10, 100); % Time from 0 to 10 seconds
x = t .* cos(t); % Example x(t)
y = t .* sin(t); % Example y(t)
z = 5*t - 0.5*9.81*t.^2; % Example z(t) simulating gravity effect
% Plotting the trajectory
figure;
plot3(x, y, z, 'LineWidth', 2);
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Trajectory of a Flying Ball');
2. If you prefer to show the trajectory being drawn over time dynamically, you can use "comet3". This doesn't animate the ball itself but gives a sense of motion to the trajectory. Replace "plot3" with "comet3" in the above code.
3. For a more detailed animation (showing a ball moving along the path), you'll need to use a loop and update the plot at each iteration. You can rotate the obtained 3D graph and take a look from various angles. Here is how you can do it:
% Example trajectory data
t = linspace(0, 10, 100); % Time from 0 to 10 seconds
x = t .* cos(t); % Example x(t)
y = t .* sin(t); % Example y(t)
z = 5*t - 0.5*9.81*t.^2; % Example z(t) simulating gravity effect
% Preparing the figure
figure;
grid on;
hold on;
axis equal;
xlabel('X');
ylabel('Y');
zlabel('Z');
xlim([min(x)-1, max(x)+1]);
ylim([min(y)-1, max(y)+1]);
zlim([min(z)-1, max(z)+1]);
title('Animating 3D Trajectory of a Flying Ball');
view(3); % Set the view to 3D
% Drawing the ball and its trajectory
ball = plot3(x(1), y(1), z(1), 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'b'); % Ball
trail = plot3(x(1), y(1), z(1), 'r', 'LineWidth', 1.5); % Trail
for k = 2:length(t)
% Update the ball's position
set(ball, 'XData', x(k), 'YData', y(k), 'ZData', z(k));
% Update the trail
set(trail, 'XData', x(1:k), 'YData', y(1:k), 'ZData', z(1:k));
drawnow; % Update the figure
pause(0.05); % Pause to control the speed of the animation
end
Here are a few documentation links that you can refer:
  1. plot3: https://www.mathworks.com/help/releases/R2021a/matlab/ref/plot3.html
  2. comet3: https://www.mathworks.com/help/releases/R2021a/matlab/ref/comet3.html
  3. drawnow: https://www.mathworks.com/help/releases/R2021a/matlab/ref/drawnow.html
  4. view: https://www.mathworks.com/help/releases/R2021a/matlab/ref/view.html
도움이 되었기를 바랍니다!

Catégories

En savoir plus sur 2차원 플롯과 3차원 플롯 dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!