I don't understand what I am doing wrong. I have to create an animation of a ball bouncing. But I have to repeat a for loop for each bounce. It also plays the animation twice. Is there a simpler way of doing this? Also, is there a way to speed up the code so that the animation does not play so slowly? I was thinking that I could write a function that would call it to repeat the animation but I am not sure.
%%Animation
clc, clf, clearg = 9.81; theta0 = 45*pi/180; v0=5; t(1) = 0; x = 0; y = 0; plot(x,y, 'o', 'MarkerFaceColor','b', 'MarkerSize', 8)axis([0 8 0 .8]); dt = 1/128;M(1) = getframefor j = 2:100 t(j) = t(j - 1) + dt; x = v0*cos(theta0)*t(j); y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2; plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8); axis([0 8 0 .8]); M(j)= getframe; endM(2) = getframet(100) = 0;v0 = v0*.8;for j = 101:198 t(j) = t(j - 1) + dt; x = v0*cos(theta0)*t(j)+2.5; y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2; plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8); axis([0 8 0 .8]); M(j)= getframe; end M(3) = getframev0 = v0*.8;t(198) = 0; for j = 199:300 t(j) = t(j - 1) + dt; x = v0*cos(theta0)*t(j)+4; y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2; plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8); axis([0 8 0 .8]); M(j)= getframe; end M(4) = getframev0 = v0*.8;t(300) = 0; for j = 199:300 t(j) = t(j - 1) + dt; x = v0*cos(theta0)*t(j)+4; y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2; plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8); axis([0 8 0 .8]); M(j)= getframe; end movie(M, 1, 1000)
Best Answer