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.

26 ビュー (過去 30 日間)
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);

採用された回答

Stephan
Stephan 2018 年 11 月 22 日
編集済み: Stephan 2018 年 11 月 22 日
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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 11 月 22 日
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.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by