how do i get the loop to graph properly?
Show older comments
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
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
joshua whitsitt
on 13 Oct 2017
joshua whitsitt
on 13 Oct 2017
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:

Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!