안녕하세요.
문의하신 내용은 영어로 답변해 드리겠습니다.
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:
- plot3: https://www.mathworks.com/help/releases/R2021a/matlab/ref/plot3.html
- comet3: https://www.mathworks.com/help/releases/R2021a/matlab/ref/comet3.html
- drawnow: https://www.mathworks.com/help/releases/R2021a/matlab/ref/drawnow.html
- view: https://www.mathworks.com/help/releases/R2021a/matlab/ref/view.html
도움이 되었기를 바랍니다!