function [x, y, z, t] = freefall( p, v, h, dt ) % Calculates the path of a particle in freefall with given initial position % and velocity, height to fall (fixed depth from origin), and time interval % (in seconds). % Returns an array of position and time (x, y, z, t) vectors. Assumes gravity % is the only external force. Calls freefall_step at each point. % If h < 0, then |x| = |y| = |z| = 0 and |t| = 1. This could be a problem. i = 1; % Index variable. while (p(3) >= -h) % Loop as long as the z variable has not fallen past h. x(i,1) = p(1); % Store current x-value. y(i,1) = p(2); % y-value. z(i,1) = p(3); % z-value. [p, v] = freefall_step( p, v, dt ); i = i + 1; % Increment our index variable. end % Now we must make a time vector to match up with the others. N = max(size(x)); % Length of x (and therefore t as well). t(1,1) = 0; % Initial time for i = 2:N t(i,1) = t(i-1,1) + dt; end % Return [x, y, z, t]