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

3 views (last 30 days)
% 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 Comment
Abeljohn
Abeljohn on 21 Apr 2024
This is what my graph looks like. As you can see the projectile arc is rotated sideways for whatever reason

Sign in to comment.

Answers (3)

David Goodmanson
David Goodmanson on 21 Apr 2024
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 on 21 Apr 2024
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 Comment
Abeljohn
Abeljohn on 25 Apr 2024
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!

Sign in to comment.


Sam Chak
Sam Chak on 26 Apr 2024
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

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!