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.

조회 수: 24 (최근 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