Plotting a projectile graph starting from 0

13 views (last 30 days)
Andrew Lew
Andrew Lew on 22 Sep 2021
Answered: Narvik on 30 Oct 2024 at 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... :)

Answers (1)

Narvik
Narvik on 30 Oct 2024 at 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!

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!