How do I write a projectile motion code that is automated and plots motion given certain heights, angles and initial velocity?

I need help writing a "for" or "while" code that is automated in a sense where it plots the projectile's motion given the heights, angles and initial velocity. I am not including drag or rolling after the initial launch since I want to learn the basics.

2 Comments

What have you done so far? What specific problems are you having with your code?
I've done this much so far. Personally, I don't have much experience so I've been getting parts from other codes and seeing if I can make it cohesive.

Sign in to comment.

 Accepted Answer

No, access the function with a simple script.
velocity=16;
figure;
hold on;
for height=1:2
for angle=30:15:60
d=projMotion(velocity,height,angle);
end
end
function d = projMotion(velocity,height,angle)
Vyi=velocity*sind(angle);
g=9.81;
tmax=(2*Vyi/g+sqrt((2*Vyi/g)^2+8*height/g))/2;
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(t,d)
end

4 Comments

David you are amazing! So with a little modification to the code, I came up with this and the plot is as shown. This is exactly what I needed for the plot. Now the question is how do I find out the maximum heights for each curve and the maximum horizontal distances?.
%Projectile Motion
velocity=16;
figure;
hold on;
for height= [5 10]
for angle= [30 45 60]
d=projMotion(velocity,height,angle);
end
end
function d = projMotion(velocity,height,angle)
Vyi=velocity*sind(angle);
g=9.81;
tmax=(2*Vyi/g+sqrt((2*Vyi/g)^2+8*height/g))/2;
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(t,d)
xlabel('Horizontal Distance (m)');
ylabel('Vertical Distance (m)');
title('Projectile Motion');
end
This is the proposed code for the max distances and max heights.
xmax = ((velocity.^2).*sin(2.*(angle)./g));
ymax = ((velocity.^2).*sin(angle)./g);
Currently plotting time vs vertical distance, if your want horizontal distance then:
velocity=16;
figure;
hold on;
c=1;
for height= [5 10]
for angle= [30 45 60]
[maxHeight(c),horDist(c)]=projMotion(velocity,height,angle);
c=c+1;
end
end
xlabel('Horizontal Distance (m)');
ylabel('Vertical Distance (m)');
title('Projectile Motion');
function [maxHeight,horDist] = projMotion(velocity,height,angle)
Vyi=velocity*sind(angle);
Vxi=velocity*cosd(angle);
g=9.81;
tmax=(2*Vyi/g+sqrt((2*Vyi/g)^2+8*height/g))/2;
horDist=tmax*Vxi;%calculates horizontal distance when hits ground==0 height
maxHeight=height+Vyi*(Vyi/g)-.5*g*(Vyi/g).^2;%calculates max height
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(Vxi*t,d)
end
Amazing! You're the man, David! You've actually taught me so much!
David's code works nicely in terms of plotting, but I think there may be an error in it. Time of flight for a projectile (tmax) is given by solving the quadratic equation for t, since this is when the projectile is either at its initial launch location, or its final location. It can be solved immediately by factoring out t and equating the bracket to zero. This results in . In code this would be:
tmax=(2*Vyi)/g;
So the MATLAB code should be:
velocity=16;
figure;
hold on;
c=1;
for height= [5 10]
for angle= [30 45 60]
[maxHeight(c),horDist(c)]=projMotion(velocity,height,angle);
c=c+1;
end
end
xlabel('Horizontal Distance (m)');
ylabel('Vertical Distance (m)');
title('Projectile Motion');
function [maxHeight,horDist] = projMotion(velocity,height,angle)
Vyi=velocity*sind(angle);
Vxi=velocity*cosd(angle);
g=9.81;
tmax=2*Vyi/g;
horDist=tmax*Vxi;%calculates horizontal distance when hits ground==0 height
maxHeight=height+Vyi*(Vyi/g)-.5*g*(Vyi/g).^2;%calculates max height
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(Vxi*t,d)
end
I have not had time to check the rest of the code.

Sign in to comment.

More Answers (1)

function d = projMotion(velocity,height,angle)
Vyi=velocity*sind(angle);
g=9.81;
tmax=(2*Vyi/g+sqrt((2*Vyi/g)^2+8*height/g))/2;
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(t,d)
end

4 Comments

It works when I input only one velocity, height and angle but how would I have it run a for loop or while loop so that it graphs at a single initial velocity, two heights and three angles all in one graph? Someone suggested a matrix set up but then again I'm very new to matlab or coding in general.
velocity=16;
figure;
hold on;
for height=1:2
for angle=30:15:60
d=projMotion(velocity,height,angle);
end
end
Davie I appreciate your help btw! Okay this is what I have so far. I know I'm doing something wrong but I can't pinpoint it.
function d = projMotion(velocity,height,angle)
velocity=16;
figure;
hold on;
for height=1:2
for angle=30:15:60
d=projMotion(velocity,height,angle);
end
end
Vyi=velocity*sind(angle);
g=9.81;
tmax=(2*Vyi/g+sqrt((2*Vyi/g)^2+8*height/g))/2;
t=0:.001:tmax;
d=height+Vyi*t-.5*g*t.^2;
plot(t,d)
end
Any chance you can explain where you got the tmax from? Im struggling to recreate it and dont know why its 8*height/g
Thanks!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 2 Feb 2022

Commented:

on 24 Jul 2024

Community Treasure Hunt

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

Start Hunting!