Why is my 3D Projectile Trajectory not being calculated properly?

2 ビュー (過去 30 日間)
Abeljohn
Abeljohn 2024 年 4 月 21 日
回答済み: Sam Chak 2024 年 4 月 26 日
% Constants
g = 9.81; % gravity
v0 = 50; % initial velocity
% Time
t = linspace(0, 5, 100); % time from 0 to 5 seconds
% Elevation and Azimuth angles
elevation = pi/4; % initial elevation angle (angle above the xz plane)
azimuth = pi/4; % initial azimuth angle (angle from the positive x-axis, counterclockwise)
% Calculate positions
x = v0 * t * cos(elevation) * cos(azimuth);
y = v0 * t * sin(elevation) - 0.5 * g * t.^2;
z = v0 * t * cos(elevation) * sin(azimuth);
% Plot 3D trajectory
figure;
plot3(x, y, z, 'LineWidth', 2);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Artillery Simulation');
grid on;
  1 件のコメント
Abeljohn
Abeljohn 2024 年 4 月 21 日
This is what my graph looks like. As you can see the projectile arc is rotated sideways for whatever reason

サインインしてコメントする。

回答 (3 件)

David Goodmanson
David Goodmanson 2024 年 4 月 21 日
Hi Abeljohn,
I think the only real issue here is trying to view the trajectory in plot3, If you let the ground be the xy plane and the height be z (which the 3d plot seems to have an easier time with), then
x = v0 * t * cos(elevation) * cos(azimuth);
y = v0 * t * cos(elevation) * sin(azimuth);
z = v0 * t * sin(elevation) - 0.5 * g * t.^2;
If you plot it and put
view([-1 -3 .8])
as the last line at the end, it looks pretty good, although it has not fallen all the way down yet.

Rishi
Rishi 2024 年 4 月 21 日
Hello Abeljohn,
I understand from your query that you want to know why the graph does not display the trajectory correctly.
From your code and the comments, I assume that you want the trajectory to be directed from the XZ plane, with the Y axis denoting the height. The reason that you get a sideways rotated trajectory is that in your graph is the orientation of the planes. The base plane in your graph is XY plane.
To obtain a graph where the projectile is launched vertically upwards from the plane, you can make the following change to your code:
% Constants
g = 9.81; % gravity
v0 = 50; % initial velocity
% Time
t = linspace(0, 5, 100); % time from 0 to 5 seconds
% Elevation and Azimuth angles
elevation = pi/4; % initial elevation angle (angle above the xz plane)
azimuth = pi/4; % initial azimuth angle (angle from the positive x-axis, counterclockwise)
% Calculate positions
x = v0 * t * cos(elevation) * cos(azimuth);
y = v0 * t * sin(elevation) - 0.5 * g * t.^2;
z = v0 * t * cos(elevation) * sin(azimuth);
% Plot 3D trajectory
figure;
plot3(x, z, y, 'LineWidth', 2);
xlabel('X');
ylabel('Z');
zlabel('Y');
title('3D Artillery Simulation');
grid on;
As you can see, the trajectory is not rotated sideways now.
You can refer to the below documentation of the 'plot3' function for more information:
Hope this helps.
  1 件のコメント
Abeljohn
Abeljohn 2024 年 4 月 25 日
Ohhh you jsut flip the z and y in the plot 3... omg that makes so much sense, why didn't I think of that. Thank you!

サインインしてコメントする。


Sam Chak
Sam Chak 2024 年 4 月 26 日
Taking a mathematical physicist's perspective into consideration, I would recommend focusing on correcting the code in the "Equations" section, rather than the "Plot" section (which may be considered a coder's shortcut).
The remainder of the code appears to be fine! 👍
% Calculate positions
x = v0 * t * cos(elevation) * cos(azimuth);
y = v0 * t * cos(elevation) * sin(azimuth); % <-- y-axis is not affected by gravity
z = v0 * t * sin(elevation) - 0.5 * g * t.^2; % <-- z-axis is affected by gravity

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by