Animating the Motion of a Ball

In an earlier homework, we considered the one-dimensional movement of a baseball, with an initial vertical velocity of 43.81 meters/second, and with gravity as a downward force with a constant acceleration of 9.8 meters/second/second.

Based on calculus, the height at time t is equal to
h(t) = v0 t - gt2/2
We calculated the height for a rane of times using vectorized operations as

g = 9.8;                              % gravity
v = 43.81;                            % initial velocity (98 mph = 43.81 m/s)

endtime = 2*v/g;                      % when it hits the ground
timestep = endtime/100;
t = 0:timestep:endtime;
height = v .* t - g .* t .^ 2 ./ 2;

animating the motion

g = 9.8;                              % gravity
v = 43.81;                            % initial velocity (98 mph = 43.81 m/s)

endtime = 2*v/g;                      % when it hits the ground
timestep = endtime/100;
t = 0:timestep:endtime;
height = v .* t - g .* t .^ 2 ./ 2;

window = [-1 1 0 max(height)];
for h = height
  plot([0], h, 'o');
  grid on;
  axis(window);                      % keep view fixed for all frames (why?)
  pause(timestep);                   % pause between frames
end

two-dimensional motion

Let's now consider the two-dimensional version of the problem, assuming that the ball's initial velocity is directed at an angle of 60-degrees from horizontal. The calculus can be applied separately on the x and y components of the motion using the formula
x(t) = vx0 t
y(t) = vy0 t - gt2/2
Here is our script animating the motion. This time, rather than clearing the plot at each step, we use hold so that we can overlay each new plot on the existing graph.

g = 9.8;      % gravity
v = 43.81;    % initial velocity (98 mph = 43.81 m/s)
angle = 60;   % measured in degrees
vx = v * cosd(angle);
vy = v * sind(angle);

endtime = 2*vy/g;
timestep = endtime/100;
t = 0:timestep:endtime;
x = vx .* t;
y = vy .* t - g .* t .^ 2 ./ 2;

window = [0 1.1*max(x) 0 1.1*max(y)];
for i = 1:length(t)
  plot(x(i), y(i), 'o');
  hold on;
  grid on;
  axis equal;                     % necessary to ensure that x and y axes are drawn with equal scale
  axis(window);
  pause(timestep);                % pause between frames
end
hold off;

Last modified: Tuesday, 03 February 2009