How do I create a projectile motion function with the input of angle which is scalar, and time which is a vector.
287 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2016 年 10 月 24 日
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.
採用された回答
Jan
2016 年 10 月 24 日
編集済み: Jan
2016 年 10 月 24 日
You can move the commands for creating the diagram from the function to the caller:
function main
figure;
AxesH = axes('NextPlot', 'add');
time = 20;
[x,y] = trajectory(10, time)
plot(x, y, 'r', 'Parent', AxesH)
[x,y] = trajectory(20, time)
plot(x, y, 'b', 'Parent', AxesH)
end
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;
end
You can vary the angle in a loop also. And if you really want to vary the time, this can be done equivalently.
1 件のコメント
Surik Ahmed
2021 年 5 月 26 日
編集済み: Surik Ahmed
2021 年 5 月 26 日
can you help me please. I have an equation by Mathieu & analytic solutions
その他の回答 (1 件)
Image Analyst
2016 年 11 月 20 日
編集済み: Image Analyst
2020 年 5 月 4 日
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 件のコメント
Image Analyst
2021 年 12 月 13 日
@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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!