Write a script which will calculate the trajectory of a ball thrown at an initial speed vo (ask the user) over a range of angles (θ = 5o − 85o in 5 degree intervals). Make a plot of your trajectories on a single graph.

27 views (last 30 days)
Not Sure where I'm going wrong here, any advice/solution will be appreciated. I'm quite new to MatLab btw
v0 = input('Enter Initial Velocity of Ball in m/s and Press Return: ');
angle=(5:5:85);
g=9.81;
t=(2*v0.*sin(angle*(pi/180)))/g;
vx = v0.*cos(angle*(pi/180));
vy = v0.*sin(angle*(pi/180));
sx = vx.*t;
sy=vy.*t-0.5*g.*t.^2;
t = 0 : 0.1:100;
plot(sx,sy);

Accepted Answer

Stephan
Stephan on 22 Nov 2018
Edited: Stephan on 22 Nov 2018
Hi,
to have all the trajectories in one plot you can use:
v0 = input('Enter Initial Velocity of Ball in m/s and Press Return: ');
angle=(5:5:85);
g=9.81;
hold on
for angle = 5:5:85
tges=(2*v0.*sin(angle.*(pi/180)))/g;
t =linspace(0,tges);
vx = v0.*cos(angle.*(pi/180));
vy = v0.*sin(angle.*(pi/180));
sx = vx.*t;
sy=vy.*t-0.5*g.*t.^2;
plot(sx,sy);
end
hold off
result:
as expected the longest distance is given by 45° angle.
Best regards
Stephan

More Answers (1)

Image Analyst
Image Analyst on 22 Nov 2018
You need to plot sx and sy separately, and don't redefine t after you've used it in your equations.
Not sure why you had the time as that, but I guess it's some equation you got in class or from a book.
v0 = input('Enter Initial Velocity of Ball in m/s and Press Return: ');
angle=(5:5:85);
g=9.81;
t=(2*v0.*sin(angle*(pi/180)))/g;
vx = v0.*cos(angle*(pi/180));
vy = v0.*sin(angle*(pi/180));
sx = vx.*t;
sy = vy.*t-0.5*g.*t.^2;
plot(t, sx, 'LineWidth', 2);
hold on;
plot(t, sy, 'LineWidth', 2);
grid on;
xlabel('time', 'FontSize', 20)
ylabel('sy or sy', 'FontSize', 20)
legend('sx', 'sy');
See my attached demo, that I've posted many times, and you can find by searching the tags projectile and trajectory. It computes just about everything you could want and allows you to set just about every initial condition you could ever want.

Categories

Find more on Programming 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!