function HW2_solutions
%
% Solutions to Homework 2, 2014
%
    close all
%
%
%
%%    Plots with straight numerical differentiation
%  Read the data files
   data_compound = csvread('HW2_data/compound_data.csv');
   data_recurve = csvread('HW2_data/recurve_data.csv'); 
   data_longbow = csvread('HW2_data/longbow_data.csv');
   static_data_compound = csvread('Hw2_data/Compound_static_data.csv');
   static_data_recurve = csvread('Hw2_data/Recurve_static_data.csv');
   static_data_longbow = csvread('Hw2_data/Longbow_static_data.csv');
%  Draw distance (in m) of each bow
   recurve_draw = 47/100;
   compound_draw = 51/100;
   longbow_draw = 54/100;
%  Arrow masses
   mass_longbow = 0.0407;
   mass_recurve = 0.031;
   mass_compound = 0.0407;
%  Compute the velocity of each bow
   [mc,n] = size(data_compound);
   for i = 1:mc-1
       dt = data_compound(i+1,1)-data_compound(i,1);
       vel_compound(i) = (data_compound(i+1,2)-data_compound(i,2))/dt;
       time_compound(i) = (data_compound(i+1,1)+data_compound(i,1))/2.;
   end
  [mr,n] = size(data_recurve);
  for i = 1:mr-1
       dt = data_recurve(i+1,1)-data_recurve(i,1);
       vel_recurve(i) = (data_recurve(i+1,2)-data_recurve(i,2))/dt;
       time_recurve(i) = (data_recurve(i+1,1)+data_recurve(i,1))/2.;
  end
      [ml,n] = size(data_longbow);
  for i = 1:ml-1
       dt = data_longbow(i+1,1)-data_longbow(i,1);
       vel_longbow(i) = (data_longbow(i+1,2)-data_longbow(i,2))/dt;
       time_longbow(i) = (data_longbow(i+1,1)+data_longbow(i,1))/2.;
   end  
%  Compute the acceleration of each bow   
   for i = 1:mc-2
       dt = time_compound(i+1)-time_compound(i);
       acc_compound(i) = (vel_compound(i+1)-vel_compound(i))/dt;
       time2_compound(i) = (time_compound(i+1)+time_compound(i))/2.;
   end
    for i = 1:mr-2
       dt = time_recurve(i+1)-time_recurve(i);
       acc_recurve(i) = (vel_recurve(i+1)-vel_recurve(i))/dt;
       time2_recurve(i) = (time_recurve(i+1)+time_recurve(i))/2.;
    end
    for i = 1:ml-2
       dt = time_longbow(i+1)-time_longbow(i);
       acc_longbow(i) = (vel_longbow(i+1)-vel_longbow(i))/dt;
       time2_longbow(i) = (time_longbow(i+1)+time_longbow(i))/2.;
    end
%  Plot the velocity and acceleration
   figure1 = figure;
   axes1 = axes('Parent',figure1,'FontSize',16);
   box(axes1,'on');
   hold(axes1,'all');
   plot(time_compound,vel_compound,'Parent',axes1,'LineWidth',2,'DisplayName','Compound bow')
   plot(time_recurve,vel_recurve,'Parent',axes1,'LineWidth',2,'Color',[1 0 0],...
    'DisplayName','Recurve bow');
   plot(time_longbow,vel_longbow,'Parent',axes1,'LineWidth',2,'Color',[0 1 0],...
    'DisplayName','Longbow');
   xlabel('Time (sec)','FontSize',16);
   ylabel('Velocity (m/s)','FontSize',16);
   title({'Velocity (numerical differentiation, no smoothing)'},'FontSize',16);
   legend1 = legend(axes1,'show');
   set(legend1,...
    'Position',[0.237709045990118 0.738001314924394 0.218928164196123 0.155161078238001],...
    'FontSize',14);
   
   figure2 = figure;
   axes2 = axes('Parent',figure2,'FontSize',16);
   box(axes2,'on');
   hold(axes2,'all');
   title({'Acceleration (compound bow, numerical differentiation)'},'FontSize',16);   
   plot(time2_compound,acc_compound,'LineWidth',2)
   xlabel('Time (sec)','FontSize',16);
   ylabel('Acceleration (m/s^2)','FontSize',16);  
   figure3 = figure
   axes3 = axes('Parent',figure3,'FontSize',16);
   box(axes3,'on');
   hold(axes3,'all');
   title({'Acceleration (recurve bow, numerical differentiation)'},'FontSize',16);   
   plot(time2_recurve,acc_recurve,'LineWidth',2,'Color',[1 0 0])
   xlabel('Time (sec)','FontSize',16);
   ylabel('Acceleration (m/s^2)','FontSize',16);  
   figure4 = figure;
   axes4 = axes('Parent',figure4,'FontSize',16);
   box(axes4,'on');
   hold(axes4,'all');
   title({'Acceleration (Longbow, numerical differentiation)'},'FontSize',16);
   plot(time2_longbow,acc_longbow,'LineWidth',2,'Color',[0 1 0])
 
%
%%    Acceleration plots with smoothing
%   
   [xData, yData] = prepareCurveData( time2_compound, acc_compound );
% Set up fittype and options.
   ft = fittype( 'smoothingspline' );
   opts = fitoptions( ft );
% Fit model to data.
   [compound_fitresult, gof] = fit( xData, yData, ft, opts );
   figure5 = figure;
   axes5 = axes('Parent',figure5,'FontSize',16);
   box(axes5,'on');
   hold(axes5,'all');
   title({'Acceleration (Compound bow, smoothed)'},'FontSize',16);
   plot(time2_compound,acc_compound,'Marker','.','LineStyle','none')
   xlim([0 time2_compound(end)]);
   plot(compound_fitresult);
   xlabel('Time (sec)','FontSize',16);
   ylabel('Acceleration (m/s^2)','FontSize',16);   
%
  [xData, yData] = prepareCurveData( time2_recurve, acc_recurve );
% Set up fittype and options.
   ft = fittype( 'smoothingspline' );
   opts = fitoptions( ft );
% Fit model to data.
   [recurve_fitresult, gof] = fit( xData, yData, ft, opts );
   figure6 = figure;
   axes6 = axes('Parent',figure6,'FontSize',16);
   box(axes6,'on');
   hold(axes6,'all');
   title({'Acceleration (Recurve bow, smoothed)'},'FontSize',16);
   plot(time2_recurve,acc_recurve,'Marker','.','LineStyle','none')
   xlim([0 time2_recurve(end)]);
   plot(recurve_fitresult)
   xlabel('Time (sec)','FontSize',16);
   ylabel('Acceleration (m/s^2)','FontSize',16);     
%
  [xData, yData] = prepareCurveData( time2_longbow, acc_longbow );
% Set up fittype and options.
   ft = fittype( 'smoothingspline' );
   opts = fitoptions( ft );
% Fit model to data.
   [longbow_fitresult, gof] = fit( xData, yData, ft, opts );
   figure7 = figure;
   axes7 = axes('Parent',figure7,'FontSize',16);
   box(axes7,'on');
   hold(axes7,'all');
   title({'Acceleration (Longbow, smoothed)'},'FontSize',16);
   plot(time2_longbow,acc_longbow,'Marker','.','LineStyle','none')
   xlim([0 time2_longbow(end)]);
   plot(longbow_fitresult)
   xlabel('Time (sec)','FontSize',16);
   ylabel('Acceleration (m/s^2)','FontSize',16);        
   
 %% The optional force-v-draw plots for longbow 
 % Convert the arrow position into draw distance  
   longbow_dynamic_draw = longbow_draw - data_longbow(2:length(data_longbow)-1,2);
%  Plot the figure
   figure8 = figure;
   axes8 = axes('Parent',figure8,'FontSize',16);
   box(axes8,'on');
   hold(axes8,'all');
   title({'Force-v-Draw for longbow'},'FontSize',16);
   plot(longbow_dynamic_draw,mass_longbow*acc_longbow,'Linewidth',2,'DisplayName','Dynamic');
   plot(static_data_longbow(:,1),static_data_longbow(:,2),'Linewidth',2,'Color',[1 0 0],'DisplayName','Static');
   xlim([-0.1 0.65]);
   legend8 = legend(axes8,'show');
   set(legend8,...
    'Position',[0.242683390345876 0.757067718606182 0.161915621436716 0.118343195266272]);
   xlabel('Draw (m)','FontSize',16);
   ylabel('Force (N)','FontSize',16);   
   
 %% The optional force-v-draw plots for compound bow 
 % Convert the arrow position into draw distance    
   compound_dynamic_draw = compound_draw - data_compound(2:length(data_compound)-1,2);
   figure9 = figure;
   axes9 = axes('Parent',figure9,'FontSize',16);
   box(axes9,'on');
   hold(axes9,'all');
   title({'Force-v-Draw for compound bow'},'FontSize',16);
   plot(compound_dynamic_draw,mass_compound*acc_compound,'Linewidth',2,'DisplayName','Dynamic');
   plot(static_data_compound(:,1),static_data_compound(:,2),'Linewidth',2,'Color',[1 0 0],'DisplayName','Static');
   xlim([-0.1 0.5]);
   legend9 = legend(axes9,'show');
   set(legend9,...
    'Position',[0.242683390345876 0.757067718606182 0.161915621436716 0.118343195266272]);
   xlabel('Draw (m)','FontSize',16);
   ylabel('Force (N)','FontSize',16);   
     
 %% The optional force-v-draw plots for recurve bow 
 % Convert the arrow position into draw distance    
   recurve_dynamic_draw = recurve_draw - data_recurve(2:length(data_recurve)-1,2);
   figure10 = figure;
   axes10 = axes('Parent',figure10,'FontSize',16);
   box(axes10,'on');
   hold(axes10,'all');
   title({'Force-v-Draw for recurve bow'},'FontSize',16);
   plot(recurve_dynamic_draw,mass_recurve*acc_recurve,'Linewidth',2,'DisplayName','Dynamic');
   plot(static_data_recurve(:,1),static_data_recurve(:,2),'Linewidth',2,'Color',[1 0 0],'DisplayName','Static');
   xlim([-0.1 0.5]);
   legend10 = legend(axes10,'show');
   set(legend10,...
    'Position',[0.242683390345876 0.757067718606182 0.161915621436716 0.118343195266272]);
   xlabel('Draw (m)','FontSize',16);
   ylabel('Force (N)','FontSize',16);   
 
   
 %% This plots the static force-v-draw curves for the three bows  
   figure11 = figure;
   axes11 = axes('Parent',figure11,'FontSize',16);
   box(axes11,'on');
   hold(axes11,'all');
   title({'Static Force-v-Draw curves'},'FontSize',16);
   plot(static_data_recurve(:,1),static_data_recurve(:,2),'Linewidth',2,'Color',[1 0 0],'DisplayName','Recurve');
   plot(static_data_longbow(:,1),static_data_longbow(:,2),'Linewidth',2,'Color',[0 1 0],'DisplayName','Longbow')   ;
   plot(static_data_compound(:,1),static_data_compound(:,2),'Linewidth',2,'Color',[0 0 1],'DisplayName','Compound bow');
   legend11 = legend(axes11,'show');
   set(legend11,...
    'Position',[0.242683390345876 0.757067718606182 0.161915621436716 0.118343195266272]);
   xlabel('Draw (m)','FontSize',16);
   ylabel('Force (N)','FontSize',16);   
   
   
end