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.

23 次查看(过去 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 CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by