How do I create a projectile motion function with the input of angle which is scalar, and time which is a vector.
Show older comments
function [x,y]=trajectory(a,time)
x0=0
y0=0
k=0
angle=a*(pi./180)
v=70
g=9.81
t=0:0.1:time
x=x0+v*cos(angle)*t;
y=y0+v*sin(angle)*t-(g*t.^2)/2
figure
plot(x,y)
end
So far I have this code, which succesfully plots the graph of a projectile at the given velocity (v) and constant (g) The input is (a) which is angle and (time) which is the amount of seconds after launch. I got stuck here because in the input (a) has to stay a scalar but (time) has to be a vector, so I can input more values for time, and the output would be more graphs with the same (a) angle, but in different times since the launch.
How can I make (time) a vector and have more plotted graphs as the output?
2 Comments
Image Analyst
on 24 Oct 2016
time is the name of a built-in function so you should not use it as the name of your variable. Call it totalTime or elapsedTime or timeOfFlight instead.
Accepted Answer
More Answers (1)
Image Analyst
on 20 Nov 2016
Edited: Image Analyst
on 4 May 2020
56 votes
OK, now that your homework problem is well over, here is my solution. Granted, it's a bit fancier than a typical beginner would do, but I'm an overachiever.
It computes just about everything that you could possibly want to know about the trajectory for a single angle. Then it computes and plots trajectories for several angles. You can delete anything that you don't need to know to make it simpler. The code is extremely well commented so you should have no trouble following it.
The projectile.m file is attached below these two images that it creates. If you like it, please "Vote" for my answer.


8 Comments
Jean Ramirez
on 29 Nov 2018
Awesome code. Thanks!
Finn Johnston
on 4 May 2020
I am assuming that velocity is in metres per second?
Image Analyst
on 4 May 2020
Yes, since acceleration is 9.8 m/sec^2, everything else is in meters and seconds. It also says that when you enter the velocity - it tells you the units to use.
I've made a few slight improvements like adding the units to the axes labels. The latest one is attached.
Xinran Deng
on 8 Aug 2020
Really awesome work!!! But could you please help me modify it to take air resistance into account?
I aim to compare the two formulae: f=kv and f=kv^2
Could you give me advice that how can I modify your version to draw the trajectory considering air resistance?
I love your presentation of nice graphs sooooo much!!!
Mena Nabil
on 20 Nov 2020
i made an account to upvote your answer thanks sir.
Anon
on 17 Dec 2020
you have absolutely saved me! thank you so much !!
Ali Madkhali
on 13 Dec 2021
how i can make the same but with an obstacles like wall in the way?
Image Analyst
on 13 Dec 2021
@Ali Madkhali you just have to define some x and y region for the obstacle, then see if the trajectory enters that region.
if x > xObstacle & y < yObstacle
% It would be inside the obstacle, so do something...
end
If it does, bounce it off or just truncate the trajectory at that point. Sorry but I don't have a demo for that.
Categories
Find more on Marine and Underwater Vehicles 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!