how do i get the loop to graph properly?

I'm having trouble getting my graph to plot properly in matlab. my code is;
clear
k=0; i=1; V0=20; theta=30; alpha=25; g=9.81;
Vx=V0*sind(theta)*cosd(alpha); Vy=V0*sind(theta)*sind(alpha);
Vz=V0*cosd(theta);
for t=0.01:0.001:2*Vz/g;
x(i)=Vx*t;
y(i)=Vy*t;
z(i)=Vz*(t)-(t)^2*g/2;
if z(i)<=0.001
k=k+1;
Vz=0.8*Vz;
i=i+1;
end
if i==6
break
end
end
plot3(x,y,z);
grid on
xlim([0 100])
ylim([0 100])
zlim([0 15])
it is supposed to display the 3D path of a ball for five bounces, however it only displays a line on an (x,y) plane.

Answers (1)

James Tursa
James Tursa on 12 Oct 2017
Edited: James Tursa on 12 Oct 2017
Move this line
i = i + 1;
outside of the if test.
And change this test
if i==6
to this instead
if k==6
You will also need to alter the for loop limits to get your bounces, since it currently ends prior to the first bounce. But once you do that, you will discover that this line does not do what you want:
Vz=0.8*Vz;
When the ball bounces, you need to change the direction of the z velocity, not just its magnitude. Also, at that point in time, Vz is not the current velocity ... the current velocity is Vz-t*g. Also this line for the z position will have to also be adjusted since it does not account for any bouncing:
z(i)=Vz*(t)-(t)^2*g/2;
If you have trouble coming up with ideas on how to accomplish the bouncing, let us know. You will need to fundamentally change that z(i)=etc line and also the Vz=0.8*etc velocity change line.

3 Comments

I did the first two. Then i had to break the i=i+1 because it went to i=4000+. now I'm lost. I've been stuck on this problem since yesterday around 5:30. The closest I have gotten is 3 and 1/2 loops.
I give up. I've spent to much time on this and I'm out of ideas. It's not worth losing any more time on my other studies. Thanks for trying to help anyways.
James Tursa
James Tursa on 13 Oct 2017
Edited: James Tursa on 13 Oct 2017
If you get more energy to work on this, post your current code and we can fix it up. E.g., making the changes I have posted and fixing the bouncing part gives:

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 12 Oct 2017

Edited:

on 13 Oct 2017

Community Treasure Hunt

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

Start Hunting!