% Simulate an attempted 3-point shot, and return miss_distance,
% which is positive for overshooting. The trajectory is plotted.
function miss_distance = miss(speed, plotstyle)

% The user may optionally control the appearance of the plotted
% trajectory.
if !exist('plotstyle','var')
   plotstyle = 'r-';
end

basket = [0, 3.05]; % Position of basket (m)
theta = 45*pi/180; % Launch angle, always the same.

v0 = speed*[cos(theta), sin(theta)]; % Initial velocity (m/s)
   % Note: 50 m/s = 112 mph
x0 = [-7.24, 2];   % Initial position (m)
   % Player holds ball 2m above floor, at NBA 3-point distance.
q0 = [x0, v0]; % Initial state vector
Nsteps = 30; % Number of integration time points to be saved.
total_time = 3; % Integration time (s)
   % This should be plenty of time to hit or miss the basket.
t = (0:Nsteps) * total_time / Nsteps; % Array of times

% Integrate the ODE for the basketball's flight
trajectory = lsode2(@basketball, q0, t);

x = trajectory(:,1);
y = trajectory(:,2);

% Find the exact margin by which the ball was over (or under)
% the basket.
height = spline(x,y,basket(1));
miss_distance = height - basket(2)

plot(x,y,plotstyle) % Plot the trajectory.
