Plotting a projectile graph starting from 0

13 ビュー (過去 30 日間)
Andrew Lew
Andrew Lew 2021 年 9 月 22 日
回答済み: Narvik 2024 年 10 月 30 日 10:33
Hi, I am currently new to matlab and am stuck on one problem given to me to solve.
I am asked to generate a projectile trajectory with initial speed of 10m/s with a launch angle of 40degrees.
I found out that in order for Matlab to work with angles in degree I have to use sind and cosd.
I've written out the initiallizations but am stuck on how to get the graph to actually show a start point from 0 all the way till the graphs ends back to 0.
This is currently what I have now:
clc;
clear all
angle = 40;
v0 = 10;
g = -9.8;
t = 0 : .01 : 100;
Ax = 0;
Ay = g;
xVelocity = v0 * cosd(angle);
yVelocity = v0 * sind(angle);
x = xVelocity .* t + (1/2) * Ax .* t.^2;
y = yVelocity .* t + (1/2) * Ay .* t.^2;
Hope some kind soul can help me out... :)

回答 (1 件)

Narvik
Narvik 2024 年 10 月 30 日 10:33
Hi Andrew Lew,
To plot the trajectory of a projectile launched at 10 m/s with a 40-degree angle, you need to calculate its path until it returns to the ground, considering gravity.
The time of flight can be calculated using the formula for vertical motion and determine x and y positions over time.
Refer to the following code to plot the trajectory of the projectile:
% Initialize parameters
angle = 40;
v0 = 10;
g = 9.8;
% Calculate initial velocities
xVelocity = v0 * cosd(angle);
yVelocity = v0 * sind(angle);
% Time of flight
t_flight = 2 * yVelocity / g;
% Time vector
t = 0:0.01:t_flight;
% Trajectory equations
x = xVelocity .* t;
y = yVelocity .* t - 0.5 * g .* t.^2;
% Plot
figure;
plot(x, y, '-b', 'LineWidth', 2);
xlabel('Horizontal Distance (m)');
ylabel('Vertical Distance (m)');
title('Projectile Trajectory');
grid on;
axis equal;
hold on;
plot(0, 0, 'ro', 'MarkerFaceColor', 'r'); % Start point
plot(x(end), y(end), 'go', 'MarkerFaceColor', 'g'); % End point
legend('Trajectory', 'Start', 'End');
hold off;
Hope this helps!

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by