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 次查看(过去 30 天)
显示 更早的评论
% 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;
回答(3 个)
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.
0 个评论
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.
Sam Chak
2024-4-26
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!