function process_baseball
%%  ENGN0040 Dynamics and Vibrations 2017
%   HW4 solutions

    close all
    baseball_mass = 0.145;
    data = csvread('test1.csv');

    time = data(:,1);
    xposition = data(:,2);
    dt = time(2)-time(1);

%   Compute the velocity using a numerical derivative of position-v-time
    for i = 1:length(time)-1
       tvel(i) = (time(i+1)+time(i))/2.;
       v(i) = (xposition(i+1)-xposition(i))/dt;   
    end
    
%   Filter the velocity 
    for i = 2:length(tvel)-1
       v(i) = 0.6*v(i-1) + 0.4*v(i); 
    end
    
%   Compute the acceleration using a numerical derivative of velocity-v-time
    for i = 1:length(tvel)-1
           tacc(i) = (tvel(i+1)+tvel(i))/2.;
           a(i) = (v(i+1)-v(i))/dt;
    end
    
%   Filter the acceleration    
    for i = 2:length(tacc)-1
       a(i) = 0.5*a(i-1) + 0.5*a(i); 
    end

    velfigure = figure;
    axes1 = axes('Parent',velfigure,'YGrid','on','XGrid','on','FontSize',14);
    box(axes1,'on');
    hold(axes1,'on');
    ylabel('velocity (m/s)');
    xlabel('Time (seconds)');
    title('Velocity -v- time');  
    plot(tvel,v/100);
   
    accfigure = figure;
    axes1 = axes('Parent',accfigure,'YGrid','on','XGrid','on','FontSize',14);
    box(axes1,'on');
    hold(axes1,'on');
    ylabel('Acceleration (m/s^2)');
    xlabel('Time (seconds)');
    title('Acceleration -v- time');  
    plot(tacc,a/100);
    
    Impulse = baseball_mass*trapz(tacc,a/100)
    
    momentumchange = baseball_mass*(51+38)
    
    % Both these estimates were obtained using the same data (position
    % -v- time) so they are not really different.
    % (the acceleration is the derivative of the acceleration data,
    % and to find the impulse we just integrate it again.  This
    % is not really different to calculating the change in velocity.
    % But the data is noisy, so the values we get depend on 
    % (1) How we smooth the accelerations; and (2) How we
    % estimate the velocity.
    % The estimate using the velocity is probably more accurate,
    % since this does not involve the step of filtering the acceleration.
    
    
end