This is what my graph looks like. As you can see the projectile arc is rotated sideways for whatever reason
Why is my 3D Projectile Trajectory not being calculated properly?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% 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;
Réponses (3)
David Goodmanson
le 21 Avr 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.
0 commentaires
Rishi
le 21 Avr 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.
Sam Chak
le 26 Avr 2024
Hi @Abeljohn
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
0 commentaires
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!