%MATEUSZ SKOCZYLAS - MOTORCYCLE DYNAMICS MODULE UWTSD - LAST EDITED
%16/03/2020 03:25
%LAP TIME SIMULATOR
%This script is designed to be fully scalable and will perform calculations
%for any motorcycle as long as valid data is supplied

clc %clear command window
clear all %clear existing workspace variables
close all %close all existing graphs
warning off; %turn off matlab warnings
load dyno_ducati_1199s %IMPORT RPM AND PWR (WHEEL HP) VARIABLES INTO WORKSPACE
load spec_ducati_1199s_v2 %LOAD SPEC DATA FOR BIKE INTO WORKSPACE (GEOMETRY ETC)
load track_donnington_gp_v2 %LOAD TRACK LAYOUT DATA INTO WORKSPACE
load real_data_lap4_exit319 %LOAD REAL LAP DATA FOR COMPARISON

%general settings
ask_velocity = 0; %1 = enable, 0 = disable | display acceleration value in command window (user input velocity required)
compare_sim_vs_real = 1; %1 = enable, 0 = disable | compare sim data with real data

%Acceleration/deacceleration multiplier
%For 1199S, reccomended: x0.85 accel, x0.67 deaccel
pos_accel_multi = 0.85; %increasing the multiplier will increase acceleration
neg_accel_multi = 0.67; %increasing the multiplier will increase deacceleration

%set lean angle for each corner
cor_b_lean = 45;
cor_d_lean = 38;
cor_f_lean = 48;
cor_h_lean = 48;
cor_j_lean = 40;
cor_l_lean = 43;
cor_n_lean = 45;
cor_p_lean = 45;
cor_r_lean = 27;
cor_t_lean = 40;
cor_v_lean = 43;
 
%FIGURE SET UP
fig_dyno = figure;
fig_vel = figure;
%fig_dri_force = figure;
fig_net_force = figure;
fig_accel_limits = figure;
fig_lapsim = figure;

fprintf('<strong>====Bike Sim====</strong>\n')
wh_circ_r = 2*pi*wh_rad_r; %rear wheel circumference m
wh_circ_f = 2*pi*wh_rad_f; %front wheel circumference m
rol = (rol_percentage/100)*mass*9.81; %rolling resistance
TORQUE = ((PWR(:,1).*5252)./RPM(:,1)).*1.3558; %torque calculation (nm)
figure(fig_dyno); %plot power & torque on a graph
hold on;
plot(RPM,PWR)
title('Power and toruqe')
xlabel('RPM')
ylabel('Power [HP]')
ylim([0 max(PWR+20)])
yyaxis right
plot(RPM,TORQUE)
ylabel('Torque [NM]')
ylim([0 max(PWR+20)])
fprintf('Max power = %.f RWHP\n',max(PWR));
fprintf('Max torque @ wheel = %.f Nm\n', max(TORQUE));
legend('Power', 'Torque')
grid on;
hold off;

%velocity calculation
gear_num = 1;
i_stop = 0;
while i_stop == 0  
    if gear_num > no_of_gears
        i_stop = 1;
    elseif gear_num == 1
        vel_1st_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_1*gr_pri*gr_final);
        vel_1st_mph = vel_1st_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max theoritical velocity in 1st gear: %.f MPH\n', max(vel_1st_mph));
    elseif gear_num == 2
        vel_2nd_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_2*gr_pri*gr_final);
        vel_2nd_mph = vel_2nd_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max theoritical velocity in 2nd gear: %.f MPH\n', max(vel_2nd_mph));
    elseif gear_num == 3
        vel_3rd_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_3*gr_pri*gr_final);
        vel_3rd_mph = vel_3rd_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max theoritical velocity in 3rd gear: %.f MPH\n', max(vel_3rd_mph));
    elseif gear_num == 4
        vel_4th_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_4*gr_pri*gr_final);
        vel_4th_mph = vel_4th_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max thereotical velocity in 4th gear: %.f MPH\n', max(vel_4th_mph));
    elseif gear_num == 5
        vel_5th_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_5*gr_pri*gr_final);
        vel_5th_mph = vel_5th_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max thereotical velocity in 5th gear: %.f MPH\n', max(vel_5th_mph));
    elseif gear_num == 6
        vel_6th_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_6*gr_pri*gr_final);
        vel_6th_mph = vel_6th_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max thereotical velocity in 6th gear: %.f MPH\n', max(vel_6th_mph));
     elseif gear_num == 7
        vel_7th_ms = ((RPM(:,1)./60).*wh_circ_r)./(gr_7*gr_pri*gr_final);
        vel_7th_mph = vel_7th_ms.*2.23694;
        gear_num=gear_num+1;
        fprintf('Max velocity in 7th gear: %.f MPH\n', max(vel_7th_mph));
    end
end
clear gear_num i_stop

%velocity graph
figure(fig_vel); 
hold on;
plot(vel_1st_mph, RPM);
if exist('vel_2nd_mph');
    plot(vel_2nd_mph, RPM);
end
if exist('vel_3rd_mph');
    plot(vel_3rd_mph, RPM);
end
if exist('vel_4th_mph')
    plot(vel_4th_mph, RPM);
end
if exist('vel_5th_mph')
    plot(vel_5th_mph, RPM);
end
if exist('vel_6th_mph')
    plot(vel_6th_mph, RPM);
end
if exist('vel_7th_mph');
    Plot(vel_7th_mph, RPM);
end
title('Velocity');
xlabel('Velocity (MPH)');
ylabel('RPM');
legend('1st', '2nd', '3rd', '4th', '5th', '6th', '7th');
grid on;
hold off;

%driving force calculation
gear_num = 1;
i_stop = 0;
HP_to_W = 735.49875;
while i_stop == 0;
    if gear_num > no_of_gears;
        i_stop = 1;
    elseif gear_num == 1;
    dri_force_1st = (PWR(:,1).*HP_to_W)./vel_1st_ms(:,1);
    gear_num=gear_num+1;
    elseif gear_num == 2;
        dri_force_2nd = (PWR(:,1).*HP_to_W)./vel_2nd_ms(:,1);
        gear_num=gear_num+1;
    elseif gear_num == 3;
        dri_force_3rd = (PWR(:,1).*HP_to_W)./vel_3rd_ms(:,1);
        gear_num=gear_num+1;
    elseif gear_num == 4;
        dri_force_4th = (PWR(:,1).*HP_to_W)./vel_4th_ms(:,1);
        gear_num=gear_num+1;
    elseif gear_num == 5;
        dri_force_5th = (PWR(:,1).*HP_to_W)./vel_5th_ms(:,1);
        gear_num=gear_num+1;
    elseif gear_num == 6;
        dri_force_6th = (PWR(:,1).*HP_to_W)./vel_6th_ms(:,1);
        gear_num=gear_num+1;
    elseif gear_num == 7;
        dri_force_7th = (PWR(:,1).*HP_to_W)./vel_7th_ms(:,1);
        gear_num=gear_num+1;
    end
end
clear i_stop gear_num;

% %driving force graph
% figure(fig_dri_force);
% hold on;
% plot(vel_1st_mph, dri_force_1st);
% if exist('dri_force_2nd');
%     plot(vel_2nd_mph, dri_force_2nd);
% end
% if exist('dri_force_3rd');
%     plot(vel_3rd_mph, dri_force_3rd);
% end
% if exist('dri_force_4th');
%     plot(vel_4th_mph, dri_force_4th);
% end
% if exist('dri_force_5th');
%     plot(vel_5th_mph, dri_force_5th);
% end
% if exist('dri_force_6th');
%     plot(vel_6th_mph, dri_force_6th);
% end
% if exist('dri_force_7th');
%     plot(vel_7th_mph, dri_force_7th);
% end
% title('Driving force only - no resistive forces')
% xlabel('Velocity (MPH)')
% ylabel('Driving Force (N)')
% ylim([0 max(dri_force_1st)+20])
% legend('1st','2nd','3rd','4th','5th','6th','7th')
% hold off;

% net force, shift points and top speed
if exist('dri_force_1st');
    drag_force_1st_tucked = ((0.5*p*cda_tucked*(vel_1st_ms(:,1).^2))+rol);
    net_force_1st = dri_force_1st(:,1)-drag_force_1st_tucked(:,1);    
end
if exist('dri_force_2nd');
    drag_force_2nd_tucked = ((0.5*p*cda_tucked*(vel_2nd_ms(:,1).^2))+rol);
    net_force_2nd = dri_force_2nd(:,1)-drag_force_2nd_tucked(:,1);
    shift_1st_to_2nd = (InterX([vel_1st_mph';net_force_1st'],[vel_2nd_mph';net_force_2nd']))';
    if isempty(shift_1st_to_2nd) == 1;
        shift_1st_to_2nd = [vel_1st_mph(end),net_force_1st(end)];
    end
    shift_rpm_1st = RPM(round(shift_1st_to_2nd(1,1),1) == round(vel_1st_mph(:,1),1)); shift_rpm_1st = shift_rpm_1st(1,1);
    fprintf('Optimal gear shift points: 1st %.f RPM, ', shift_rpm_1st)
    start_rpm_2nd = shift_rpm_1st*(gr_2/gr_1); start_rpm_2nd = ceil(start_rpm_2nd/5)*5;
end
if exist('dri_force_3rd');
    drag_force_3rd_tucked = ((0.5*p*cda_tucked*(vel_3rd_ms(:,1).^2))+rol);
    net_force_3rd = dri_force_3rd(:,1)-drag_force_3rd_tucked(:,1);
    shift_2nd_to_3rd = (InterX([vel_2nd_mph';net_force_2nd'],[vel_3rd_mph';net_force_3rd']))';
    if isempty(shift_2nd_to_3rd) == 1;
        shift_2nd_to_3rd = [vel_2nd_mph(end),net_force_2nd(end)];
    end
    shift_rpm_2nd = RPM(round(shift_2nd_to_3rd(1,1),1) == round(vel_2nd_mph(:,1),1)); shift_rpm_2nd = shift_rpm_2nd(1,1);
    fprintf('2nd %.f RPM, ', shift_rpm_2nd)
    start_rpm_3rd = shift_rpm_2nd*(gr_3/gr_2); start_rpm_3rd = ceil(start_rpm_3rd/5)*5;
end
if exist('dri_force_4th');
    drag_force_4th_tucked = ((0.5*p*cda_tucked*(vel_4th_ms(:,1).^2))+rol);   
    net_force_4th = dri_force_4th(:,1)-drag_force_4th_tucked(:,1);
    shift_3rd_to_4th = (InterX([vel_3rd_mph';net_force_3rd'],[vel_4th_mph';net_force_4th']))';
    if isempty(shift_3rd_to_4th) == 1;
        shift_3rd_to_4th = [vel_3rd_mph(end),net_force_3rd(end)];
    end
    shift_rpm_3rd = RPM(round(shift_3rd_to_4th(1,1),1) == round(vel_3rd_mph(:,1),1)); shift_rpm_3rd = shift_rpm_3rd(1,1);
    fprintf('3rd %.f RPM, ', shift_rpm_3rd)
    start_rpm_4th = shift_rpm_3rd*(gr_4/gr_3); start_rpm_4th = ceil(start_rpm_4th/5)*5;
    
end
if exist('dri_force_5th');
    drag_force_5th_tucked = ((0.5*p*cda_tucked*(vel_5th_ms(:,1).^2))+rol);
    net_force_5th = dri_force_5th(:,1)-drag_force_5th_tucked(:,1);
    shift_4th_to_5th = (InterX([vel_4th_mph';net_force_4th'],[vel_5th_mph';net_force_5th']))';
    if isempty(shift_4th_to_5th) == 1;
        shift_4th_to_5th = [vel_4th_mph(end),net_force_4th(end)];
    end
    shift_rpm_4th = RPM(round(shift_4th_to_5th(1,1),1) == round(vel_4th_mph(:,1),1)); shift_rpm_4th = shift_rpm_4th(1,1);
    fprintf('4th %.f RPM, ', shift_rpm_4th)
    start_rpm_5th = shift_rpm_4th*(gr_5/gr_4); start_rpm_5th = ceil(start_rpm_5th/5)*5;
end
if exist('dri_force_6th');
    drag_force_6th_tucked = ((0.5*p*cda_tucked*(vel_6th_ms(:,1).^2))+rol);
    net_force_6th = dri_force_6th(:,1)-drag_force_6th_tucked(:,1);
    shift_5th_to_6th = (InterX([vel_5th_mph';net_force_5th'],[vel_6th_mph';net_force_6th']))';
    if isempty(shift_5th_to_6th) == 1;
        shift_5th_to_6th = [vel_5th_mph(end),net_force_5th(end)];
    end
    shift_rpm_5th = RPM(round(shift_5th_to_6th(1,1),1) == round(vel_5th_mph(:,1),1)); shift_rpm_5th = shift_rpm_5th(1,1);
    fprintf('5th %.f RPM, ', shift_rpm_5th)
    start_rpm_6th = shift_rpm_5th*(gr_6/gr_5); start_rpm_6th = ceil(start_rpm_6th/5)*5;
end
if exist('dri_force_7th');
    drag_force_7th_tucked = ((0.5*p*cda_tucked*(vel_7th_ms(:,1).^2))+rol);
    net_force_7th = dri_force_7th(:,1)-drag_force_7th_tucked(:,1);
    shift_6th_to_7th = (InterX([vel_6th_mph';net_force_6th'],[vel_7th_mph';net_force_7th']))';
    if isempty(shift_6th_to_7th) == 1;
        shift_6th_to_7th = [vel_6th_mph(end),net_force_6th(end)];
    end
    shift_rpm_6th = RPM(round(shift_6th_to_7th(1,1),1) == round(vel_6th_mph(:,1),1)); shift_rpm_6th = shift_rpm_6th(1,1);
    fprintf('6th %.f RPM, ', shift_rpm_6th)
    start_rpm_7th = shift_rpm_6th*(gr_7/gr_6); start_rpm_7th = ceil(start_rpm_7th/5)*5;
end

%net force & shift point graph
figure(fig_net_force);
hold on;
plot(vel_1st_mph, net_force_1st,'b');
if exist('net_force_2nd');
    plot(shift_1st_to_2nd(1,1),(shift_1st_to_2nd(1,2)),'b*')
    plot(vel_2nd_mph, net_force_2nd,'k');
end
if exist('net_force_3rd');
    plot(shift_2nd_to_3rd(1,1),(shift_2nd_to_3rd(1,2)),'k*')
    plot(vel_3rd_mph, net_force_3rd,'r');
end
if exist('net_force_4th');
    plot(shift_3rd_to_4th(1,1),(shift_3rd_to_4th(1,2)),'r*')
    plot(vel_4th_mph, net_force_4th,'g');
end
if exist('net_force_5th');
    plot(shift_4th_to_5th(1,1),(shift_4th_to_5th(1,2)),'g*')
    plot(vel_5th_mph, net_force_5th,'m');
end
if exist('net_force_6th');
    plot(shift_5th_to_6th(1,1),(shift_5th_to_6th(1,2)),'m*')
    plot(vel_6th_mph, net_force_6th,'c');
end
if exist('net_force_7th');
    plot(shift_6th_to_7th(1,1),(shift_6th_to_7th(1,2)),'c*')
    plot(vel_7th_mph, net_force_7th);
end
title('Net force')
xlabel('Velocity (MPH)')
ylabel('Net Driving Force (N)')
ylim([0 max(net_force_1st)+0.1*max(net_force_1st)])
if no_of_gears == 1
    legend('1st')
elseif no_of_gears == 2
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd')
elseif no_of_gears == 3
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd', ['RPM: ' num2str(shift_rpm_2nd)], '3rd')
elseif no_of_gears == 4
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd', ['RPM: ' num2str(shift_rpm_2nd)], '3rd', ['RPM: ' num2str(shift_rpm_3rd)], '4th')
elseif no_of_gears == 5
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd', ['RPM: ' num2str(shift_rpm_2nd)], '3rd', ['RPM: ' num2str(shift_rpm_3rd)], '4th', ['RPM: ' num2str(shift_rpm_4th)], '5th')
elseif no_of_gears == 6
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd', ['RPM: ' num2str(shift_rpm_2nd)], '3rd', ['RPM: ' num2str(shift_rpm_3rd)], '4th', ['RPM: ' num2str(shift_rpm_4th)], '5th', ['RPM: ' num2str(shift_rpm_5th)], '6th')
elseif no_of_gears == 7
    legend('1st', ['RPM: ' num2str(shift_rpm_1st)], '2nd', ['RPM: ' num2str(shift_rpm_2nd)], '3rd', ['RPM: ' num2str(shift_rpm_3rd)], '4th', ['RPM: ' num2str(shift_rpm_4th)], '5th', ['RPM: ' num2str(shift_rpm_5th)], '6th',  ['RPM: ' (num2str(shift_rpm_6th))], '7th')
end
grid on
hold off

% reduced mass calculation
gear_num = 1;
i_stop = 0;
while i_stop == 0;
    if gear_num > no_of_gears;
        i_stop = 1;
    elseif gear_num == 1;
        rmass_1st = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_1*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 2;
        rmass_2nd = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_2*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 3;
        rmass_3rd = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_3*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 4;
        rmass_4th = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_4*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 5;
        rmass_5th = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_5*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 6;
        rmass_6th = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_6*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    elseif gear_num == 7;
        rmass_7th = mass+If*(1/wh_rad_f)^2+Ir*(1/wh_rad_r)^2+Is*(gr_final*(1/wh_rad_r))^2+Ip*(gr_pri*gr_final*(1/wh_rad_r))^2+Im*(gr_final*gr_7*gr_pri*(1/wh_rad_r))^2;
        gear_num = gear_num+1;
    end
end
clear gear_num i_stop

%acceleration: traction limited
 gear_num = 1;
 i_stop = 0;
 while i_stop == 0;
     if gear_num > no_of_gears;
         i_stop = 1;
     elseif gear_num == 1;
         tmp_tract_ltd_accel_1st = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_1st_tucked(:,1).*geo_p+tract_coeff.*drag_force_1st_tucked(:,1)*geo_h)/(geo_p*rmass_1st-tract_coeff*rmass_1st*geo_h);
         [low_idx ~] = find(RPM(1,1));
         if exist('shift_rpm_1st') == 1
            [high_idx ~] = find(RPM == shift_rpm_1st);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         tract_ltd_accel_1st = tmp_tract_ltd_accel_1st(low_idx:high_idx);
         vel_x1 = vel_1st_ms(low_idx:high_idx);
         tract_ltd_accel_all = [tract_ltd_accel_1st vel_x1];
         clear low_idx high_idx;
         gear_num = gear_num + 1;
     elseif gear_num == 2;
         tmp_tract_ltd_accel_2nd = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_2nd_tucked(:,1).*geo_p+tract_coeff.*drag_force_2nd_tucked(:,1)*geo_h)/(geo_p*rmass_2nd-tract_coeff*rmass_2nd*geo_h);
         [low_idx ~] = find(RPM == start_rpm_2nd);
         if exist('shift_rpm_2nd') == 1
            [high_idx ~] = find(RPM == shift_rpm_2nd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_2nd = tmp_tract_ltd_accel_2nd(low_idx:high_idx);
         vel_x2 = vel_2nd_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd];       
         tmp_stack_vel = [vel_x1;vel_x2];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 3;
         tmp_tract_ltd_accel_3rd = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_3rd_tucked(:,1).*geo_p+tract_coeff.*drag_force_3rd_tucked(:,1)*geo_h)/(geo_p*rmass_3rd-tract_coeff*rmass_3rd*geo_h);
         [low_idx ~] = find(RPM == start_rpm_3rd);
         if exist('shift_rpm_3rd') == 1
            [high_idx ~] = find(RPM == shift_rpm_3rd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_3rd = tmp_tract_ltd_accel_3rd(low_idx:high_idx);
         vel_x3 = vel_3rd_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd; tract_ltd_accel_3rd];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 4;
         tmp_tract_ltd_accel_4th = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_4th_tucked(:,1).*geo_p+tract_coeff.*drag_force_4th_tucked(:,1)*geo_h)/(geo_p*rmass_4th-tract_coeff*rmass_4th*geo_h);
         [low_idx ~] = find(RPM == start_rpm_4th);
         if exist('shift_rpm_4th') == 1
            [high_idx ~] = find(RPM == shift_rpm_4th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_4th = tmp_tract_ltd_accel_4th(low_idx:high_idx);
         vel_x4 = vel_4th_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd; tract_ltd_accel_3rd;tract_ltd_accel_4th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 5;
         tmp_tract_ltd_accel_5th = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_5th_tucked(:,1).*geo_p+tract_coeff.*drag_force_5th_tucked(:,1)*geo_h)/(geo_p*rmass_5th-tract_coeff*rmass_5th*geo_h);
         [low_idx ~] = find(RPM == start_rpm_5th);
         if exist('shift_rpm_5th') == 1
            [high_idx ~] = find(RPM == shift_rpm_5th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_5th = tmp_tract_ltd_accel_5th(low_idx:high_idx);
         vel_x5 = vel_5th_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd; tract_ltd_accel_3rd;tract_ltd_accel_4th;tract_ltd_accel_5th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 6;
         tmp_tract_ltd_accel_6th = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_6th_tucked(:,1).*geo_p+tract_coeff.*drag_force_6th_tucked(:,1)*geo_h)/(geo_p*rmass_6th-tract_coeff*rmass_6th*geo_h);
         [low_idx ~] = find(RPM == start_rpm_6th);
         if exist('shift_rpm_6th') == 1
            [high_idx ~] = find(RPM == shift_rpm_6th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_6th = tmp_tract_ltd_accel_6th(low_idx:high_idx);
         vel_x6 = vel_6th_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd; tract_ltd_accel_3rd;tract_ltd_accel_4th;tract_ltd_accel_5th;tract_ltd_accel_6th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 7;
         tmp_tract_ltd_accel_7th = (tract_coeff*mass*9.81*(geo_p-geo_b)-drag_force_7th_tucked(:,1).*geo_p+tract_coeff.*drag_force_7th_tucked(:,1)*geo_h)/(geo_p*rmass_7th-tract_coeff*rmass_7th*geo_h);
         [low_idx ~] = find(RPM == start_rpm_7th);
         if exist('shift_rpm_7th') == 1
            [high_idx ~] = find(RPM == shift_rpm_7th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         tract_ltd_accel_7th = tmp_tract_ltd_accel_7th(low_idx:high_idx);
         vel_x7 = vel_7th_ms(low_idx:high_idx,1);
         tmp_stack_force = [tract_ltd_accel_1st;tract_ltd_accel_2nd; tract_ltd_accel_3rd;tract_ltd_accel_4th;tract_ltd_accel_5th;tract_ltd_accel_6th;tract_ltd_accel_7th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6;vel_x7];
         tract_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     end
 end
clear gear_num i_stop

%acceleration: wheelie limited
 gear_num = 1;
 i_stop = 0;
 while i_stop == 0;
     if gear_num > no_of_gears;
         i_stop = 1;
     elseif gear_num == 1;
         tmp_wheelie_ltd_accel_1st = (mass/rmass_1st)*9.81*(geo_b/geo_h)-(drag_force_1st_tucked(:,1)./rmass_1st);
         [low_idx ~] = find(RPM(1,1));
         if exist('shift_rpm_1st') == 1
            [high_idx ~] = find(RPM == shift_rpm_1st);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         wheelie_ltd_accel_1st = tmp_wheelie_ltd_accel_1st(low_idx:high_idx);
         vel_x1 = vel_1st_ms(low_idx:high_idx);
         wheelie_ltd_accel_all = [wheelie_ltd_accel_1st vel_x1];
         clear low_idx high_idx;
         gear_num = gear_num + 1;
     elseif gear_num == 2;
         tmp_wheelie_ltd_accel_2nd = (mass/rmass_2nd)*9.81*(geo_b/geo_h)-(drag_force_2nd_tucked(:,1)./rmass_2nd);
         [low_idx ~] = find(RPM == start_rpm_2nd);
         if exist('shift_rpm_2nd') == 1
            [high_idx ~] = find(RPM == shift_rpm_2nd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         wheelie_ltd_accel_2nd = tmp_wheelie_ltd_accel_2nd(low_idx:high_idx);
         vel_x2 = vel_2nd_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd];       
         tmp_stack_vel = [vel_x1;vel_x2];
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 3;
         tmp_wheelie_ltd_accel_3rd = (mass/rmass_3rd)*9.81*(geo_b/geo_h)-(drag_force_3rd_tucked(:,1)./rmass_3rd);
         [low_idx ~] = find(RPM == start_rpm_3rd);
         if exist('shift_rpm_3rd') == 1
            [high_idx ~] = find(RPM == shift_rpm_3rd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end         
         wheelie_ltd_accel_3rd = tmp_wheelie_ltd_accel_3rd(low_idx:high_idx);
         vel_x3 = vel_3rd_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd;wheelie_ltd_accel_3rd];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3];
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 4;
         tmp_wheelie_ltd_accel_4th = (mass/rmass_4th)*9.81*(geo_b/geo_h)-(drag_force_4th_tucked(:,1)./rmass_4th);
         [low_idx ~] = find(RPM == start_rpm_4th);
         if exist('shift_rpm_4th') == 1
            [high_idx ~] = find(RPM == shift_rpm_4th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end         
         wheelie_ltd_accel_4th = tmp_wheelie_ltd_accel_4th(low_idx:high_idx);
         vel_x4 = vel_4th_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd;wheelie_ltd_accel_3rd;wheelie_ltd_accel_4th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4];
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;   
     elseif gear_num == 5;
         tmp_wheelie_ltd_accel_5th = (mass/rmass_5th)*9.81*(geo_b/geo_h)-(drag_force_5th_tucked(:,1)./rmass_5th);
         [low_idx ~] = find(RPM == start_rpm_5th);
         if exist('shift_rpm_5th') == 1
            [high_idx ~] = find(RPM == shift_rpm_5th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         wheelie_ltd_accel_5th = tmp_wheelie_ltd_accel_5th(low_idx:high_idx);
         vel_x5 = vel_5th_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd;wheelie_ltd_accel_3rd;wheelie_ltd_accel_4th;wheelie_ltd_accel_5th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5];
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 6;
         tmp_wheelie_ltd_accel_6th = (mass/rmass_6th)*9.81*(geo_b/geo_h)-(drag_force_6th_tucked(:,1)./rmass_6th);
         [low_idx ~] = find(RPM == start_rpm_6th);
         if exist('shift_rpm_6th') == 1
            [high_idx ~] = find(RPM == shift_rpm_6th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         wheelie_ltd_accel_6th = tmp_wheelie_ltd_accel_6th(low_idx:high_idx);
         vel_x6 = vel_6th_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd;wheelie_ltd_accel_3rd;wheelie_ltd_accel_4th;wheelie_ltd_accel_5th;wheelie_ltd_accel_6th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6];
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 7;
         tmp_wheelie_ltd_accel_7th = (mass/rmass_7th)*9.81*(geo_b/geo_h)-(drag_force_7th_tucked(:,1)./rmass_7th);
         [low_idx ~] = find(RPM == start_rpm_7th);
         if exist('shift_rpm_7th') == 1
            [high_idx ~] = find(RPM == shift_rpm_7th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         wheelie_ltd_accel_7th = tmp_wheelie_ltd_accel_7th(low_idx:high_idx);
         vel_x7 = vel_7th_ms(low_idx:high_idx,1);
         tmp_stack_force = [wheelie_ltd_accel_1st;wheelie_ltd_accel_2nd;wheelie_ltd_accel_3rd;wheelie_ltd_accel_4th;wheelie_ltd_accel_5th;wheelie_ltd_accel_6th;wheelie_ltd_accel_7th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6;vel_x7]
         wheelie_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;         
         gear_num = gear_num + 1;
     end
 end
 clear gear_num i_stop
 
%acceleration: net driving force limited
 gear_num = 1;
 i_stop = 0;
 while i_stop == 0;
     if gear_num > no_of_gears;
         i_stop = 1;
     elseif gear_num == 1;
         tmp_driforce_ltd_accel_1st = net_force_1st(:,1)./rmass_1st;
         [low_idx ~] = find(RPM(1,1));
         if exist('shift_rpm_1st') == 1
            [high_idx ~] = find(RPM == shift_rpm_1st);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_1st = tmp_driforce_ltd_accel_1st(low_idx:high_idx);
         vel_x1 = vel_1st_ms(low_idx:high_idx);
         driforce_ltd_accel_all = [driforce_ltd_accel_1st vel_x1];
         clear low_idx high_idx;
         gear_num = gear_num + 1;
     elseif gear_num == 2;
         tmp_driforce_ltd_accel_2nd = net_force_2nd(:,1)./rmass_2nd;
         [low_idx ~] = find(RPM == start_rpm_2nd);
         if exist('shift_rpm_2nd') == 1
            [high_idx ~] = find(RPM == shift_rpm_2nd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_2nd = tmp_driforce_ltd_accel_2nd(low_idx:high_idx);
         vel_x2 = vel_2nd_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd];       
         tmp_stack_vel = [vel_x1;vel_x2];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 3;
         tmp_driforce_ltd_accel_3rd = net_force_3rd(:,1)./rmass_3rd;
         [low_idx ~] = find(RPM == start_rpm_3rd);
         if exist('shift_rpm_3rd') == 1
            [high_idx ~] = find(RPM == shift_rpm_3rd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_3rd = tmp_driforce_ltd_accel_3rd(low_idx:high_idx);
         vel_x3 = vel_3rd_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd;driforce_ltd_accel_3rd];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 4;
         tmp_driforce_ltd_accel_4th = net_force_4th(:,1)./rmass_4th;
         [low_idx ~] = find(RPM == start_rpm_4th);
         if exist('shift_rpm_4th') == 1
            [high_idx ~] = find(RPM == shift_rpm_4th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_4th = tmp_driforce_ltd_accel_4th(low_idx:high_idx);
         vel_x4 = vel_4th_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd;driforce_ltd_accel_3rd;driforce_ltd_accel_4th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 5;
         tmp_driforce_ltd_accel_5th = net_force_5th(:,1)./rmass_5th;
         [low_idx ~] = find(RPM == start_rpm_5th);
         if exist('shift_rpm_5th') == 1
            [high_idx ~] = find(RPM == shift_rpm_5th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_5th = tmp_driforce_ltd_accel_5th(low_idx:high_idx);
         vel_x5 = vel_5th_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd;driforce_ltd_accel_3rd;driforce_ltd_accel_4th;driforce_ltd_accel_5th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 6;
         tmp_driforce_ltd_accel_6th = net_force_6th(:,1)./rmass_6th;
         [low_idx ~] = find(RPM == start_rpm_6th);
         if exist('shift_rpm_6th') == 1
             [high_idx ~] = find(RPM == shift_rpm_6th);
         else
             [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_6th = tmp_driforce_ltd_accel_6th(low_idx:high_idx);
         vel_x6 = vel_6th_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd;driforce_ltd_accel_3rd;driforce_ltd_accel_4th;driforce_ltd_accel_5th;driforce_ltd_accel_6th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 7;
         tmp_driforce_ltd_accel_7th = net_force_7th(:,1)./rmass_7th;
         [low_idx ~] = find(RPM == start_rpm_7th);
         if exist('shift_rpm_7th') == 1
             [high_idx ~] = find(RPM == shift_rpm_7th);
         else
             [high_idx ~] = find(RPM == RPM(end,1));
         end
         driforce_ltd_accel_7th = tmp_driforce_ltd_accel_7th(low_idx:high_idx);
         vel_x7 = vel_7th_ms(low_idx:high_idx,1);
         tmp_stack_force = [driforce_ltd_accel_1st;driforce_ltd_accel_2nd;driforce_ltd_accel_3rd;driforce_ltd_accel_4th;driforce_ltd_accel_5th;driforce_ltd_accel_6th;driforce_ltd_accel_7th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6;vel_x7];
         driforce_ltd_accel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;        
         gear_num = gear_num + 1;
     end
 end
 clear gear_num i_stop
 driforce_ltd_accel_all(driforce_ltd_accel_all(:,1) < 0) = 0;
 
%acceleration: Combined acceleration limits
 idx = 1;
 i_stop = 0;
 combined_accel_limit = zeros(1);
 while i_stop == 0
    [idx_end ~] = size(driforce_ltd_accel_all);
    [idx_end2 ~] = size(combined_accel_limit);
    if idx_end == idx_end2
        i_stop = 1;
    elseif driforce_ltd_accel_all(idx,1) <= wheelie_ltd_accel_all(idx,1) && driforce_ltd_accel_all(idx,1) <= tract_ltd_accel_all(idx,1);
       combined_accel_limit(idx,1) = pos_accel_multi.*driforce_ltd_accel_all(idx,1);
       combined_accel_limit(idx,2) = driforce_ltd_accel_all(idx,2);
       idx = idx+1;
    elseif wheelie_ltd_accel_all(idx,1) <= driforce_ltd_accel_all(idx,1) && wheelie_ltd_accel_all(idx,1) <= tract_ltd_accel_all(idx,1);
       combined_accel_limit(idx,1) = pos_accel_multi.*wheelie_ltd_accel_all(idx,1);
       combined_accel_limit(idx,2) = wheelie_ltd_accel_all(idx,2);
       idx = idx+1;
    elseif tract_ltd_accel_all(idx,1) <= driforce_ltd_accel_all(idx,1) && tract_ltd_accel_all(idx,1) <= wheelie_ltd_accel_all(idx,1);
       combined_accel_limit(idx,1) = pos_accel_multi.*tract_ltd_accel_all(idx,1);
       combined_accel_limit(idx,2) = tract_ltd_accel_all(idx,2);
       idx = idx+1;
    end
 end
 clear idx idx_end idx_end2;
 
 if driforce_ltd_accel_all(end,1) > 0
     fprintf('\nGearing is limiting top speed!!!')
     idx = size(driforce_ltd_accel_all); idx = idx(1,1);
 else
     idx = find(driforce_ltd_accel_all(:,1) == 0); idx = idx(1,1);
 end
 realistic_top_speed_ms = driforce_ltd_accel_all(idx,2);
 realistic_top_speed_mph = (driforce_ltd_accel_all(idx,2))*2.237;
 fprintf('\nRealistic top speed: %.f MPH\n', realistic_top_speed_mph);
 clear idx;
 
%if enabled, display acceleration (in command window) at specified velocity
 if ask_velocity == 1;
    usr_vel_ms = input('Enter speed (m/s) to retrive acceleration: ');
    [idx ~] = find(round(driforce_ltd_accel_all(:,2),1) == round(usr_vel_ms,1)); idx = idx(1,1);
    fprintf('Acceleration @ %.1fm/s: %.1fm/s^2\n', usr_vel_ms, driforce_ltd_accel_all(idx,1))
 end
 
%deacceleration: endo limited
gear_num = 1;
 i_stop = 0;
 while i_stop == 0;
     if gear_num > no_of_gears;
         i_stop = 1;
     elseif gear_num == 1;
         drag_force_1st_braking = ((0.5*p*cda_braking*(vel_1st_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_1st = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_1st_braking(:,1))/rmass_1st);
         [low_idx ~] = find(RPM(1,1));
         if exist('shift_rpm_1st') == 1
            [high_idx ~] = find(RPM == shift_rpm_1st);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         endo_ltd_deaccel_1st = tmp_endo_ltd_deaccel_1st(low_idx:high_idx);
         vel_x1 = vel_1st_ms(low_idx:high_idx);
         endo_ltd_deaccel_all = [endo_ltd_deaccel_1st vel_x1];
         clear low_idx high_idx;
         gear_num = gear_num + 1;
     elseif gear_num == 2;
         drag_force_2nd_braking = ((0.5*p*cda_braking*(vel_2nd_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_2nd = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_2nd_braking(:,1))/rmass_2nd);
         [low_idx ~] = find(RPM == start_rpm_2nd);
         if exist('shift_rpm_2nd') == 1
            [high_idx ~] = find(RPM == shift_rpm_2nd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end   
         endo_ltd_deaccel_2nd = tmp_endo_ltd_deaccel_2nd(low_idx:high_idx);
         vel_x2 = vel_2nd_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd];       
         tmp_stack_vel = [vel_x1;vel_x2];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
      elseif gear_num == 3;
         drag_force_3rd_braking = ((0.5*p*cda_braking*(vel_3rd_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_3rd = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_3rd_braking(:,1))/rmass_3rd);
         [low_idx ~] = find(RPM == start_rpm_3rd);
         if exist('shift_rpm_3rd') == 1
            [high_idx ~] = find(RPM == shift_rpm_3rd);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end         
         endo_ltd_deaccel_3rd = tmp_endo_ltd_deaccel_3rd(low_idx:high_idx);
         vel_x3 = vel_3rd_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd;endo_ltd_deaccel_3rd];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 4;
         drag_force_4th_braking = ((0.5*p*cda_braking*(vel_4th_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_4th = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_4th_braking(:,1))/rmass_4th);
         [low_idx ~] = find(RPM == start_rpm_4th);
         if exist('shift_rpm_4th') == 1
            [high_idx ~] = find(RPM == shift_rpm_4th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end         
         endo_ltd_deaccel_4th = tmp_endo_ltd_deaccel_4th(low_idx:high_idx);
         vel_x4 = vel_4th_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd;endo_ltd_deaccel_3rd;endo_ltd_deaccel_4th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;   
     elseif gear_num == 5;
         drag_force_5th_braking = ((0.5*p*cda_braking*(vel_5th_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_5th = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_5th_braking(:,1))/rmass_5th);
         [low_idx ~] = find(RPM == start_rpm_5th);
         if exist('shift_rpm_5th') == 1
            [high_idx ~] = find(RPM == shift_rpm_5th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         endo_ltd_deaccel_5th = tmp_endo_ltd_deaccel_5th(low_idx:high_idx);
         vel_x5 = vel_5th_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd;endo_ltd_deaccel_3rd;endo_ltd_deaccel_4th;endo_ltd_deaccel_5th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 6;
         drag_force_6th_braking = ((0.5*p*cda_braking*(vel_6th_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_6th = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_6th_braking(:,1))/rmass_6th);
         [low_idx ~] = find(RPM == start_rpm_6th);
         if exist('shift_rpm_6th') == 1
            [high_idx ~] = find(RPM == shift_rpm_6th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         endo_ltd_deaccel_6th = tmp_endo_ltd_deaccel_6th(low_idx:high_idx);
         vel_x6 = vel_6th_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd;endo_ltd_deaccel_3rd;endo_ltd_deaccel_4th;endo_ltd_deaccel_5th;endo_ltd_deaccel_6th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;
         gear_num = gear_num + 1;
     elseif gear_num == 7;
         drag_force_7th_braking = ((0.5*p*cda_braking*(vel_7th_ms(:,1).^2))+rol);
         tmp_endo_ltd_deaccel_7th = -((((mass*9.81*(geo_p-geo_b))/geo_h)+drag_force_7th_braking(:,1))/rmass_7th);
         [low_idx ~] = find(RPM == start_rpm_7th);
         if exist('shift_rpm_7th') == 1
            [high_idx ~] = find(RPM == shift_rpm_7th);
         else
            [high_idx ~] = find(RPM == RPM(end,1));
         end
         endo_ltd_deaccel_7th = tmp_endo_ltd_deaccel_7th(low_idx:high_idx);
         vel_x7 = vel_7th_ms(low_idx:high_idx,1);
         tmp_stack_force = [endo_ltd_deaccel_1st;endo_ltd_deaccel_2nd;endo_ltd_deaccel_3rd;endo_ltd_deaccel_4th;endo_ltd_deaccel_5th;endo_ltd_deaccel_6th;endo_ltd_deaccel_7th];       
         tmp_stack_vel = [vel_x1;vel_x2;vel_x3;vel_x4;vel_x5;vel_x6;vel_x7];
         endo_ltd_deaccel_all = [tmp_stack_force tmp_stack_vel];
         clear low_idx high_idx tmp_stack_force tmp_stack_vel;         
         gear_num = gear_num + 1;
     end
 end
 clear gear_num i_stop
 
%deacceleration: traction limited
traction_ltd_deaccel = -(tract_coeff*9.81);
[idx ~] = size(endo_ltd_deaccel_all);
traction_ltd_deaccel_all(1:idx,1) = traction_ltd_deaccel;
traction_ltd_deaccel_all(1:idx,2) = endo_ltd_deaccel_all(:,2);

%deacceleration: combined limits
 idx = 1;
 i_stop = 0;
 combined_deaccel_limit = zeros(1);
 while i_stop == 0
    [idx_end ~] = size(traction_ltd_deaccel_all);
    [idx_end2 ~] = size(combined_deaccel_limit);
    if idx_end == idx_end2
        i_stop = 1;
    elseif endo_ltd_deaccel_all(idx,1) >= traction_ltd_deaccel_all(idx,1)
       combined_deaccel_limit(idx,1) = neg_accel_multi.*endo_ltd_deaccel_all(idx,1);
       combined_deaccel_limit(idx,2) = endo_ltd_deaccel_all(idx,2);
       idx = idx+1;
    elseif traction_ltd_deaccel_all(idx,1) >= endo_ltd_deaccel_all(idx,1)
       combined_deaccel_limit(idx,1) = neg_accel_multi.*traction_ltd_deaccel_all(idx,1);
       combined_deaccel_limit(idx,2) = traction_ltd_deaccel_all(idx,2);
       idx = idx+1;
    end
 end
 clear idx idx_end idx_end2;
 
%plot acceleration and deacceleration
figure(fig_accel_limits);
subplot(2,1,1)
plot(driforce_ltd_accel_all(:,2), driforce_ltd_accel_all(:,1));
hold on;
plot(wheelie_ltd_accel_all(:,2), wheelie_ltd_accel_all(:,1));
plot(tract_ltd_accel_all(:,2), tract_ltd_accel_all(:,1));
plot(combined_accel_limit(:,2), combined_accel_limit(:,1));
title('Acceleration limits');
xlabel('velocity m/s');
ylabel('acceleration m/s^2');
%ylim([0 max(driforce_ltd_accel_all(:,1))]);
legend('Engine limited','Wheelie limited', 'Traction limited', 'Overall/adjusted limit')
grid on;
hold off;
subplot(2,1,2)
plot(endo_ltd_deaccel_all(:,2), endo_ltd_deaccel_all(:,1))
hold on
plot(traction_ltd_deaccel_all(:,2), traction_ltd_deaccel_all(:,1))
plot(combined_deaccel_limit(:,2),combined_deaccel_limit(:,1))
title('Deacceleration limits')
ylabel('Acceleration m/s^2')
xlabel('Velocity m/s')
legend('Endo limited', 'Traction limited', 'Overall/adjusted limit')
grid on;
hold off;

%%%LAP SIM%%%%
fprintf('\n<strong>====Lap Sim====</strong>\n')
if pos_accel_multi ~= 1
    fprintf('Acceleration multiplier <strong>enabled</strong>: %.2f\n', pos_accel_multi)
end
if neg_accel_multi ~= 1
    fprintf('Deacceleration multiplier <strong>enabled</strong>: %.2f\n', neg_accel_multi)
end
if compare_sim_vs_real == 1
    fprintf('Real vs sim comparison mode <strong>enabled</strong>\n')
end
fprintf('Corner distance: %.00f m\n', corner_dist)
fprintf('Straight distance: %.00f m\n', straight_dist)
fprintf('Total distance: %.00f m\n', total_dist)

%run up - section v
runup_cor_vel_v = sqrt(tand(cor_v_lean)*track_cor_v_rad*9.81);

%run up - section w
init_val = 1;
tmp_runup_dist = 1;
tmp_row_counter = 1;
for i = 1:track_str_w_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(runup_cor_vel_v,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end
tmp_accel = combined_accel_limit(tmp_idx_vel,1);
if init_val == 1
   vel_1m_on = sqrt(runup_cor_vel_v^2+2*tmp_accel*1);
else
   vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
end
runup_w(tmp_row_counter,1) = ([tmp_runup_dist]);
runup_w(tmp_row_counter,2) = ([vel_1m_on]);
runup_w(tmp_row_counter,3) = ([tmp_accel]);
tmp_runup_dist = tmp_runup_dist+1;
tmp_row_counter = tmp_row_counter+1;
init_val = 0;
end
start_line_vel = runup_w(end,2);
start_line_accel = runup_w(end,3); 
clear init_val tmp_runup_dist tmp_row_counter tmp_idx_vel tmp_accel vel_1m_on i
fprintf('\nStart line velocity (calculated): %.1fm/s\n', start_line_vel)
if compare_sim_vs_real == 1
    fprintf('Start line velocity (real): %.1fm/s\n', real_all(1,2))
end
%%Start line run up finished%%%

%>Sector A STR
init_val = 1;
dist_covered = 0;
tmp_row_counter = 1;
str_a_results(tmp_row_counter,1) = ([dist_covered]) ;
str_a_results(tmp_row_counter,2) = ([start_line_vel]);
str_a_results(tmp_row_counter,3) = ([start_line_accel]);
str_a_results(tmp_row_counter,4) = ([0]);
tmp_row_counter = tmp_row_counter+1;
dist_covered = dist_covered+1;
for i = 1:track_str_a_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(start_line_vel,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   if init_val == 1
       vel_1m_on = sqrt(start_line_vel^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_a_results(tmp_row_counter,1) = ([dist_covered]);
   str_a_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_a_results(tmp_row_counter,3) = ([tmp_accel]);
   str_a_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_a_results(tmp_row_counter-1,4)]);
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end
  
%>Sector B COR
 idx = 1;
 for i = 1:track_cor_b_dist
   cor_b_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_b_results(1:track_cor_b_dist,2) = sqrt(tand(cor_b_lean)*track_cor_b_rad*9.81);
desired_cor_b_vel = cor_b_results(1,2);
cor_b_results(1:track_cor_b_dist,3) = 0;
if str_a_results(end,2) > (cor_b_results(1,2))       
    [idx_size ~] = size(str_a_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_a_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_a_results(end,2) = sqrt(str_a_results(idx-1,2)^2+2*tmp_deaccel);
    str_a_results(end,3) = tmp_deaccel;
    str_a_results(end,4) = (1/str_a_results(end,2))+str_a_results(end-1,4);
    while round(str_a_results(end,2),1) >  round(cor_b_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_a_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_a_results(idx,2) = sqrt(str_a_results(idx-1,2)^2+2*tmp_deaccel);
        str_a_results(idx,3) = tmp_deaccel;
        str_a_results(idx,4) = (1/str_a_results(idx,2))+str_a_results(idx-1,4);
        brake_begin_str_a =  idx-1; %-1 only for section A
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_a_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_a_results(idx_update,2) = sqrt(str_a_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_a_results(idx_update,3) = tmp_deaccel;
             str_a_results(idx_update,4) = (1/str_a_results(idx_update,2))+str_a_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_b_dist
        if first_vel_val == 1
            cor_b_results(1,4) = ([(1/cor_b_results(1,2))+str_a_results(end,4)]);
            first_vel_val = 0;
        else
            cor_b_results(counter,4) = ([(1/cor_b_results(counter,2))+cor_b_results(counter-1,4)]);
            counter = counter+1;
        end
    end

elseif round(str_a_results(end,2),1) <=  round(cor_b_results(1,2),1)
    brake_begin_str_a = 0;
    cor_b_results(1:track_cor_b_dist,2) = str_a_results(end,2);
    cor_b_results(1:track_cor_b_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_b_dist
        if first_vel_val == 1
            cor_b_results(1,4) = ([(1/cor_b_results(1,2))+str_a_results(end,4)]);
            first_vel_val = 0;
        else
            cor_b_results(counter,4) = ([(1/cor_b_results(counter,2))+cor_b_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector A & B
str_a_brake_dist = track_str_a_dist-brake_begin_str_a;
str_a_brake_perc = (str_a_brake_dist/track_str_a_dist)*100;
str_a_avg_vel = mean(str_a_results); str_a_avg_vel = str_a_avg_vel(1,2);
fprintf('<strong>SIM SECTOR A</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_a_results(end,4), str_a_brake_dist, str_a_brake_perc, str_a_avg_vel, min(str_a_results(:,3)), max(str_a_results(:,3)))
if round(str_a_results(end,2)) <  floor(desired_cor_b_vel)
    fprintf(2, '!!!SIM SECTOR B - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_b_vel, str_a_results(end,2))
end
fprintf('<strong>SIM SECTOR B</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_b_results(end,4)-str_a_results(end,4), cor_b_results(1,2))  

%>Sector C STR
init_val = 1;
dist_covered = cor_b_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_c_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_b_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_b_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_c_results(tmp_row_counter,1) = ([dist_covered]);
   str_c_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_c_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_c_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_b_results(end,4)]);
   else
       str_c_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_c_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector D COR
 idx = 1;
 for i = 1:track_cor_d_dist
   cor_d_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_d_results(1:track_cor_d_dist,2) = sqrt(track_cor_d_rad*9.81*tand(cor_d_lean)); 
desired_cor_d_vel = cor_d_results(1,2);
cor_d_results(1:track_cor_d_dist,3) = 0;
if str_c_results(end,2) > (cor_d_results(1,2))       
    [idx_size ~] = size(str_c_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_c_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_c_results(end,2) = sqrt(str_c_results(idx-1,2)^2+2*tmp_deaccel);
    str_c_results(end,3) = tmp_deaccel;
    str_c_results(end,4) = (1/str_c_results(end,2))+str_c_results(end-1,4);
    while round(str_c_results(end,2),1) >  round(cor_d_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_c_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_c_results(idx,2) = sqrt(str_c_results(idx-1,2)^2+2*tmp_deaccel);
        str_c_results(idx,3) = tmp_deaccel;
        str_c_results(idx,4) = (1/str_c_results(idx,2))+str_c_results(idx-1,4);
        brake_begin_str_c =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_c_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_c_results(idx_update,2) = sqrt(str_c_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_c_results(idx_update,3) = tmp_deaccel;
             str_c_results(idx_update,4) = (1/str_c_results(idx_update,2))+str_c_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_d_dist
        if first_vel_val == 1
            cor_d_results(1,4) = ([(1/cor_d_results(1,2))+str_c_results(end,4)]);
            first_vel_val = 0;
        else
            cor_d_results(counter,4) = ([(1/cor_d_results(counter,2))+cor_d_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_c_results(end,2),1) <=  round(cor_d_results(1,2),1)
    brake_begin_str_c = 0;
    cor_d_results(1:track_cor_d_dist,2) = str_c_results(end,2);
    cor_d_results(1:track_cor_d_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_d_dist
        if first_vel_val == 1
            cor_d_results(1,4) = ([(1/cor_d_results(1,2))+str_c_results(end,4)]);
            first_vel_val = 0;
        else
            cor_d_results(counter,4) = ([(1/cor_d_results(counter,2))+cor_d_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector C & D
str_c_brake_dist = track_str_c_dist-brake_begin_str_c;
str_c_brake_perc = (str_c_brake_dist/track_str_c_dist)*100;
str_c_avg_vel = mean(str_c_results); str_c_avg_vel = str_c_avg_vel(1,2);
fprintf('<strong>SIM SECTOR C</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_c_results(end,4)-cor_b_results(end,4), str_c_brake_dist, str_c_brake_perc, str_c_avg_vel, min(str_c_results(:,3)), max(str_c_results(:,3)))
if round(str_c_results(end,2)) < floor(desired_cor_d_vel)
    fprintf(2, '!!!SIM SECTOR D - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_d_vel, str_c_results(end,2))
end
fprintf('<strong>SIM SECTOR D</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_d_results(end,4)-str_c_results(end,4), cor_d_results(1,2))

%>Sector E STR
init_val = 1;
dist_covered = cor_d_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_e_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_d_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_d_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_e_results(tmp_row_counter,1) = ([dist_covered]);
   str_e_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_e_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_e_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_d_results(end,4)]);
   else
       str_e_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_e_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector F COR
 idx = 1;
 for i = 1:track_cor_f_dist
   cor_f_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_f_results(1:track_cor_f_dist,2) = sqrt(track_cor_f_rad*9.81*tand(cor_f_lean)); 
desired_cor_f_vel = cor_f_results(1,2);
cor_f_results(1:track_cor_f_dist,3) = 0;
if str_e_results(end,2) > (cor_f_results(1,2))       
    [idx_size ~] = size(str_e_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_e_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_e_results(end,2) = sqrt(str_e_results(idx-1,2)^2+2*tmp_deaccel);
    str_e_results(end,3) = tmp_deaccel;
    str_e_results(end,4) = (1/str_e_results(end,2))+str_e_results(end-1,4);
    while round(str_e_results(end,2),1) >  round(cor_f_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_e_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_e_results(idx,2) = sqrt(str_e_results(idx-1,2)^2+2*tmp_deaccel);
        str_e_results(idx,3) = tmp_deaccel;
        str_e_results(idx,4) = (1/str_e_results(idx,2))+str_e_results(idx-1,4);
        brake_begin_str_e =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_e_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_e_results(idx_update,2) = sqrt(str_e_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_e_results(idx_update,3) = tmp_deaccel;
             str_e_results(idx_update,4) = (1/str_e_results(idx_update,2))+str_e_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_f_dist
        if first_vel_val == 1
            cor_f_results(1,4) = ([(1/cor_f_results(1,2))+str_e_results(end,4)]);
            first_vel_val = 0;
        else
            cor_f_results(counter,4) = ([(1/cor_f_results(counter,2))+cor_f_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_e_results(end,2),1) <=  round(cor_f_results(1,2),1)
    brake_begin_str_e = 0;
    cor_f_results(1:track_cor_f_dist,2) = str_e_results(end,2);
    cor_f_results(1:track_cor_f_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_f_dist
        if first_vel_val == 1
            cor_f_results(1,4) = ([(1/cor_f_results(1,2))+str_e_results(end,4)]);
            first_vel_val = 0;
        else
            cor_f_results(counter,4) = ([(1/cor_f_results(counter,2))+cor_f_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector E & F
str_e_brake_dist = track_str_e_dist-brake_begin_str_e;
str_e_brake_perc = (str_e_brake_dist/track_str_e_dist)*100;
str_e_avg_vel = mean(str_e_results); str_e_avg_vel = str_e_avg_vel(1,2);
fprintf('<strong>SIM SECTOR E</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_e_results(end,4)-cor_d_results(end,4), str_e_brake_dist, str_e_brake_perc, str_e_avg_vel, min(str_e_results(:,3)), max(str_e_results(:,3)))
if round(str_e_results(end,2)) < floor(desired_cor_f_vel)
    fprintf(2, '!!!SIM SECTOR F - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_f_vel, str_e_results(end,2))
end
fprintf('<strong>SIM SECTOR F</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_f_results(end,4)-str_e_results(end,4), cor_f_results(1,2))

%>Sector G STR
init_val = 1;
dist_covered = cor_f_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_g_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_f_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_f_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_g_results(tmp_row_counter,1) = ([dist_covered]);
   str_g_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_g_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_g_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_f_results(end,4)]);
   else
       str_g_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_g_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector H COR
 idx = 1;
 for i = 1:track_cor_h_dist
   cor_h_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_h_results(1:track_cor_h_dist,2) = sqrt(track_cor_h_rad*9.81*tand(cor_h_lean)); 
desired_cor_h_vel = cor_h_results(1,2);
cor_h_results(1:track_cor_h_dist,3) = 0;
if str_g_results(end,2) > (cor_h_results(1,2))       
    [idx_size ~] = size(str_g_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_g_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_g_results(end,2) = sqrt(str_g_results(idx-1,2)^2+2*tmp_deaccel);
    str_g_results(end,3) = tmp_deaccel;
    str_g_results(end,4) = (1/str_g_results(end,2))+str_g_results(end-1,4);
    while round(str_g_results(end,2),1) >  round(cor_h_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_g_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_g_results(idx,2) = sqrt(str_g_results(idx-1,2)^2+2*tmp_deaccel);
        str_g_results(idx,3) = tmp_deaccel;
        str_g_results(idx,4) = (1/str_g_results(idx,2))+str_g_results(idx-1,4);
        brake_begin_str_g =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_g_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_g_results(idx_update,2) = sqrt(str_g_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_g_results(idx_update,3) = tmp_deaccel;
             str_g_results(idx_update,4) = (1/str_g_results(idx_update,2))+str_g_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_h_dist
        if first_vel_val == 1
            cor_h_results(1,4) = ([(1/cor_h_results(1,2))+str_g_results(end,4)]);
            first_vel_val = 0;
        else
            cor_h_results(counter,4) = ([(1/cor_h_results(counter,2))+cor_h_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_g_results(end,2),1) <=  round(cor_h_results(1,2),1)
    brake_begin_str_g = 0;
    cor_h_results(1:track_cor_h_dist,2) = str_g_results(end,2);
    cor_h_results(1:track_cor_h_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_h_dist
        if first_vel_val == 1
            cor_h_results(1,4) = ([(1/cor_h_results(1,2))+str_g_results(end,4)]);
            first_vel_val = 0;
        else
            cor_h_results(counter,4) = ([(1/cor_h_results(counter,2))+cor_h_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector G & H
str_g_brake_dist = track_str_g_dist-brake_begin_str_g;
str_g_brake_perc = (str_g_brake_dist/track_str_g_dist)*100;
str_g_avg_vel = mean(str_g_results); str_g_avg_vel = str_g_avg_vel(1,2);
fprintf('<strong>SIM SECTOR G</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_g_results(end,4)-cor_f_results(end,4), str_g_brake_dist, str_g_brake_perc, str_g_avg_vel, min(str_g_results(:,3)), max(str_g_results(:,3)))
if round(str_g_results(end,2)) < floor(desired_cor_h_vel)
    fprintf(2, '!!!SIM SECTOR H - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_h_vel, str_g_results(end,2))
end
fprintf('<strong>SIM SECTOR H</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_h_results(end,4)-str_g_results(end,4), cor_h_results(1,2))

%>Sector I STR
init_val = 1;
dist_covered = cor_h_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_i_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_h_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_h_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_i_results(tmp_row_counter,1) = ([dist_covered]);
   str_i_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_i_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_i_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_h_results(end,4)]);
   else
       str_i_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_i_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector J COR
 idx = 1;
 for i = 1:track_cor_j_dist
   cor_j_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_j_results(1:track_cor_j_dist,2) = sqrt(track_cor_j_rad*9.81*tand(cor_j_lean)); 
desired_cor_j_vel = cor_j_results(1,2);
cor_j_results(1:track_cor_j_dist,3) = 0;
if str_i_results(end,2) > (cor_j_results(1,2))       
    [idx_size ~] = size(str_i_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_i_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_i_results(end,2) = sqrt(str_i_results(idx-1,2)^2+2*tmp_deaccel);
    str_i_results(end,3) = tmp_deaccel;
    str_i_results(end,4) = (1/str_i_results(end,2))+str_i_results(end-1,4);
    while round(str_i_results(end,2),1) >  round(cor_j_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_i_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_i_results(idx,2) = sqrt(str_i_results(idx-1,2)^2+2*tmp_deaccel);
        str_i_results(idx,3) = tmp_deaccel;
        str_i_results(idx,4) = (1/str_i_results(idx,2))+str_i_results(idx-1,4);
        brake_begin_str_i =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_i_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_i_results(idx_update,2) = sqrt(str_i_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_i_results(idx_update,3) = tmp_deaccel;
             str_i_results(idx_update,4) = (1/str_i_results(idx_update,2))+str_i_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_j_dist
        if first_vel_val == 1
            cor_j_results(1,4) = ([(1/cor_j_results(1,2))+str_i_results(end,4)]);
            first_vel_val = 0;
        else
            cor_j_results(counter,4) = ([(1/cor_j_results(counter,2))+cor_j_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_i_results(end,2),1) <=  round(cor_j_results(1,2),1)
    brake_begin_str_i = 0;
    cor_j_results(1:track_cor_j_dist,2) = str_i_results(end,2);
    cor_j_results(1:track_cor_j_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_j_dist
        if first_vel_val == 1
            cor_j_results(1,4) = ([(1/cor_j_results(1,2))+str_i_results(end,4)]);
            first_vel_val = 0;
        else
            cor_j_results(counter,4) = ([(1/cor_j_results(counter,2))+cor_j_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector I & J
str_i_brake_dist = track_str_i_dist-brake_begin_str_i;
str_i_brake_perc = (str_i_brake_dist/track_str_i_dist)*100;
str_i_avg_vel = mean(str_i_results); str_i_avg_vel = str_i_avg_vel(1,2);
fprintf('<strong>SIM SECTOR I</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_i_results(end,4)-cor_h_results(end,4), str_i_brake_dist, str_i_brake_perc, str_i_avg_vel, min(str_i_results(:,3)), max(str_i_results(:,3)))
if round(str_i_results(end,2)) < floor(desired_cor_j_vel)
    fprintf(2, '!!!SIM SECTOR J - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_j_vel, str_i_results(end,2))
end
fprintf('<strong>SIM SECTOR J</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_j_results(end,4)-str_i_results(end,4), cor_j_results(1,2))

%>Sector K STR
init_val = 1;
dist_covered = cor_j_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_k_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_j_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_j_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_k_results(tmp_row_counter,1) = ([dist_covered]);
   str_k_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_k_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_k_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_j_results(end,4)]);
   else
       str_k_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_k_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector L COR
 idx = 1;
 for i = 1:track_cor_l_dist
   cor_l_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_l_results(1:track_cor_l_dist,2) = sqrt(track_cor_l_rad*9.81*tand(cor_l_lean)); 
desired_cor_l_vel = cor_l_results(1,2);
cor_l_results(1:track_cor_l_dist,3) = 0;
if str_k_results(end,2) > (cor_l_results(1,2))       
    [idx_size ~] = size(str_k_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_k_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_k_results(end,2) = sqrt(str_k_results(idx-1,2)^2+2*tmp_deaccel);
    str_k_results(end,3) = tmp_deaccel;
    str_k_results(end,4) = (1/str_k_results(end,2))+str_k_results(end-1,4);
    while round(str_k_results(end,2),1) >  round(cor_l_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_k_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_k_results(idx,2) = sqrt(str_k_results(idx-1,2)^2+2*tmp_deaccel);
        str_k_results(idx,3) = tmp_deaccel;
        str_k_results(idx,4) = (1/str_k_results(idx,2))+str_k_results(idx-1,4);
        brake_begin_str_k =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_k_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_k_results(idx_update,2) = sqrt(str_k_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_k_results(idx_update,3) = tmp_deaccel;
             str_k_results(idx_update,4) = (1/str_k_results(idx_update,2))+str_k_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_l_dist
        if first_vel_val == 1
            cor_l_results(1,4) = ([(1/cor_l_results(1,2))+str_k_results(end,4)]);
            first_vel_val = 0;
        else
            cor_l_results(counter,4) = ([(1/cor_l_results(counter,2))+cor_l_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_k_results(end,2),1) <=  round(cor_l_results(1,2),1)
    brake_begin_str_k = 0;
    cor_l_results(1:track_cor_l_dist,2) = str_k_results(end,2);
    cor_l_results(1:track_cor_l_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_l_dist
        if first_vel_val == 1
            cor_l_results(1,4) = ([(1/cor_l_results(1,2))+str_k_results(end,4)]);
            first_vel_val = 0;
        else
            cor_l_results(counter,4) = ([(1/cor_l_results(counter,2))+cor_l_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector K & L
str_k_brake_dist = track_str_k_dist-brake_begin_str_k;
str_k_brake_perc = (str_k_brake_dist/track_str_k_dist)*100;
str_k_avg_vel = mean(str_k_results); str_k_avg_vel = str_k_avg_vel(1,2);
fprintf('<strong>SIM SECTOR K</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_k_results(end,4)-cor_j_results(end,4), str_k_brake_dist, str_k_brake_perc, str_k_avg_vel, min(str_k_results(:,3)), max(str_k_results(:,3)))
if round(str_k_results(end,2)) < floor(desired_cor_l_vel)
    fprintf(2, '!!!SIM SECTOR L - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_l_vel, str_k_results(end,2))
end
fprintf('<strong>SIM SECTOR L</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_l_results(end,4)-str_k_results(end,4), cor_l_results(1,2))

%>Sector M STR
init_val = 1;
dist_covered = cor_l_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_m_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_l_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_l_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_m_results(tmp_row_counter,1) = ([dist_covered]);
   str_m_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_m_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_m_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_l_results(end,4)]);
   else
       str_m_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_m_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector N COR
 idx = 1;
 for i = 1:track_cor_n_dist
   cor_n_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_n_results(1:track_cor_n_dist,2) = sqrt(track_cor_n_rad*9.81*tand(cor_n_lean)); 
desired_cor_n_vel = cor_n_results(1,2);
cor_n_results(1:track_cor_n_dist,3) = 0;
if str_m_results(end,2) > (cor_n_results(1,2))       
    [idx_size ~] = size(str_m_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_m_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_m_results(end,2) = sqrt(str_m_results(idx-1,2)^2+2*tmp_deaccel);
    str_m_results(end,3) = tmp_deaccel;
    str_m_results(end,4) = (1/str_m_results(end,2))+str_m_results(end-1,4);
    while round(str_m_results(end,2),1) >  round(cor_n_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_m_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_m_results(idx,2) = sqrt(str_m_results(idx-1,2)^2+2*tmp_deaccel);
        str_m_results(idx,3) = tmp_deaccel;
        str_m_results(idx,4) = (1/str_m_results(idx,2))+str_m_results(idx-1,4);
        brake_begin_str_m =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_m_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_m_results(idx_update,2) = sqrt(str_m_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_m_results(idx_update,3) = tmp_deaccel;
             str_m_results(idx_update,4) = (1/str_m_results(idx_update,2))+str_m_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_n_dist
        if first_vel_val == 1
            cor_n_results(1,4) = ([(1/cor_n_results(1,2))+str_m_results(end,4)]);
            first_vel_val = 0;
        else
            cor_n_results(counter,4) = ([(1/cor_n_results(counter,2))+cor_n_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_m_results(end,2),1) <=  round(cor_n_results(1,2),1)
    brake_begin_str_m = 0;
    cor_n_results(1:track_cor_n_dist,2) = str_m_results(end,2);
    cor_n_results(1:track_cor_n_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_n_dist
        if first_vel_val == 1
            cor_n_results(1,4) = ([(1/cor_n_results(1,2))+str_m_results(end,4)]);
            first_vel_val = 0;
        else
            cor_n_results(counter,4) = ([(1/cor_n_results(counter,2))+cor_n_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector M & N
str_m_brake_dist = track_str_m_dist-brake_begin_str_m;
str_m_brake_perc = (str_m_brake_dist/track_str_m_dist)*100;
str_m_avg_vel = mean(str_m_results); str_m_avg_vel = str_m_avg_vel(1,2);
fprintf('<strong>SIM SECTOR M</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_m_results(end,4)-cor_l_results(end,4), str_m_brake_dist, str_m_brake_perc, str_m_avg_vel, min(str_m_results(:,3)), max(str_m_results(:,3)))
if round(str_m_results(end,2)) < floor(desired_cor_n_vel)
    fprintf(2, '!!!SIM SECTOR N - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_n_vel, str_m_results(end,2))
end
fprintf('<strong>SIM SECTOR N</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_n_results(end,4)-str_m_results(end,4), cor_n_results(1,2))

%>Sector O STR
init_val = 1;
dist_covered = cor_n_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_o_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_n_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_n_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_o_results(tmp_row_counter,1) = ([dist_covered]);
   str_o_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_o_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_o_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_n_results(end,4)]);
   else
       str_o_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_o_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector P COR
 idx = 1;
 for i = 1:track_cor_p_dist
   cor_p_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_p_results(1:track_cor_p_dist,2) = sqrt(track_cor_p_rad*9.81*tand(cor_p_lean)); 
desired_cor_p_vel = cor_p_results(1,2);
cor_p_results(1:track_cor_p_dist,3) = 0;
if str_o_results(end,2) > (cor_p_results(1,2))       
    [idx_size ~] = size(str_o_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_o_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_o_results(end,2) = sqrt(str_o_results(idx-1,2)^2+2*tmp_deaccel);
    str_o_results(end,3) = tmp_deaccel;
    str_o_results(end,4) = (1/str_o_results(end,2))+str_o_results(end-1,4);
    while round(str_o_results(end,2),1) >  round(cor_p_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_o_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_o_results(idx,2) = sqrt(str_o_results(idx-1,2)^2+2*tmp_deaccel);
        str_o_results(idx,3) = tmp_deaccel;
        str_o_results(idx,4) = (1/str_o_results(idx,2))+str_o_results(idx-1,4);
        brake_begin_str_o =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_o_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_o_results(idx_update,2) = sqrt(str_o_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_o_results(idx_update,3) = tmp_deaccel;
             str_o_results(idx_update,4) = (1/str_o_results(idx_update,2))+str_o_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_p_dist
        if first_vel_val == 1
            cor_p_results(1,4) = ([(1/cor_p_results(1,2))+str_o_results(end,4)]);
            first_vel_val = 0;
        else
            cor_p_results(counter,4) = ([(1/cor_p_results(counter,2))+cor_p_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_o_results(end,2),1) <=  round(cor_p_results(1,2),1)
    brake_begin_str_o = 0;
    cor_p_results(1:track_cor_p_dist,2) = str_o_results(end,2);
    cor_p_results(1:track_cor_p_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_p_dist
        if first_vel_val == 1
            cor_p_results(1,4) = ([(1/cor_p_results(1,2))+str_o_results(end,4)]);
            first_vel_val = 0;
        else
            cor_p_results(counter,4) = ([(1/cor_p_results(counter,2))+cor_p_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector O & P
str_o_brake_dist = track_str_o_dist-brake_begin_str_o;
str_o_brake_perc = (str_o_brake_dist/track_str_o_dist)*100;
str_o_avg_vel = mean(str_o_results); str_o_avg_vel = str_o_avg_vel(1,2);
fprintf('<strong>SIM SECTOR O</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_o_results(end,4)-cor_n_results(end,4), str_o_brake_dist, str_o_brake_perc, str_o_avg_vel, min(str_o_results(:,3)), max(str_o_results(:,3)))
if round(str_o_results(end,2)) < floor(desired_cor_p_vel)
    fprintf(2, '!!!SIM SECTOR P - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_p_vel, str_o_results(end,2))
end
fprintf('<strong>SIM SECTOR P</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_p_results(end,4)-str_o_results(end,4), cor_p_results(1,2))

%>Sector Q STR
init_val = 1;
dist_covered = cor_p_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_q_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_p_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_p_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_q_results(tmp_row_counter,1) = ([dist_covered]);
   str_q_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_q_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_q_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_p_results(end,4)]);
   else
       str_q_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_q_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector R COR
 idx = 1;
 for i = 1:track_cor_r_dist
   cor_r_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_r_results(1:track_cor_r_dist,2) = sqrt(track_cor_r_rad*9.81*tand(cor_r_lean)); 
desired_cor_r_vel = cor_r_results(1,2);
cor_r_results(1:track_cor_r_dist,3) = 0;
if str_q_results(end,2) > (cor_r_results(1,2))       
    [idx_size ~] = size(str_q_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_q_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_q_results(end,2) = sqrt(str_q_results(idx-1,2)^2+2*tmp_deaccel);
    str_q_results(end,3) = tmp_deaccel;
    str_q_results(end,4) = (1/str_q_results(end,2))+str_q_results(end-1,4);
    while round(str_q_results(end,2),1) >  round(cor_r_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_q_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_q_results(idx,2) = sqrt(str_q_results(idx-1,2)^2+2*tmp_deaccel);
        str_q_results(idx,3) = tmp_deaccel;
        str_q_results(idx,4) = (1/str_q_results(idx,2))+str_q_results(idx-1,4);
        brake_begin_str_q =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_q_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_q_results(idx_update,2) = sqrt(str_q_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_q_results(idx_update,3) = tmp_deaccel;
             str_q_results(idx_update,4) = (1/str_q_results(idx_update,2))+str_q_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_r_dist
        if first_vel_val == 1
            cor_r_results(1,4) = ([(1/cor_r_results(1,2))+str_q_results(end,4)]);
            first_vel_val = 0;
        else
            cor_r_results(counter,4) = ([(1/cor_r_results(counter,2))+cor_r_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_q_results(end,2),1) <=  round(cor_r_results(1,2),1)
    brake_begin_str_q = 0;
    cor_r_results(1:track_cor_r_dist,2) = str_q_results(end,2);
    cor_r_results(1:track_cor_r_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_r_dist
        if first_vel_val == 1
            cor_r_results(1,4) = ([(1/cor_r_results(1,2))+str_q_results(end,4)]);
            first_vel_val = 0;
        else
            cor_r_results(counter,4) = ([(1/cor_r_results(counter,2))+cor_r_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector Q & R
str_q_brake_dist = track_str_q_dist-brake_begin_str_q;
str_q_brake_perc = (str_q_brake_dist/track_str_q_dist)*100;
str_q_avg_vel = mean(str_q_results); str_q_avg_vel = str_q_avg_vel(1,2);
fprintf('<strong>SIM SECTOR Q</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_q_results(end,4)-cor_p_results(end,4), str_q_brake_dist, str_q_brake_perc, str_q_avg_vel, min(str_q_results(:,3)), max(str_q_results(:,3)))
if round(str_q_results(end,2)) < floor(desired_cor_r_vel)
    fprintf(2, '!!!SIM SECTOR R - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_r_vel, str_q_results(end,2))
end
fprintf('<strong>SIM SECTOR R</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_r_results(end,4)-str_q_results(end,4), cor_r_results(1,2))

%>Sector S STR
init_val = 1;
dist_covered = cor_r_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_s_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_r_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_r_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_s_results(tmp_row_counter,1) = ([dist_covered]);
   str_s_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_s_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_s_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_r_results(end,4)]);
   else
       str_s_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_s_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector T COR
 idx = 1;
 for i = 1:track_cor_t_dist
   cor_t_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_t_results(1:track_cor_t_dist,2) = sqrt(track_cor_t_rad*9.81*tand(cor_t_lean)); 
desired_cor_t_vel = cor_t_results(1,2);
cor_t_results(1:track_cor_t_dist,3) = 0;
if str_s_results(end,2) > (cor_t_results(1,2))       
    [idx_size ~] = size(str_s_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_s_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_s_results(end,2) = sqrt(str_s_results(idx-1,2)^2+2*tmp_deaccel);
    str_s_results(end,3) = tmp_deaccel;
    str_s_results(end,4) = (1/str_s_results(end,2))+str_s_results(end-1,4);
    while round(str_s_results(end,2),1) >  round(cor_t_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_s_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_s_results(idx,2) = sqrt(str_s_results(idx-1,2)^2+2*tmp_deaccel);
        str_s_results(idx,3) = tmp_deaccel;
        str_s_results(idx,4) = (1/str_s_results(idx,2))+str_s_results(idx-1,4);
        brake_begin_str_s =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_s_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_s_results(idx_update,2) = sqrt(str_s_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_s_results(idx_update,3) = tmp_deaccel;
             str_s_results(idx_update,4) = (1/str_s_results(idx_update,2))+str_s_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_t_dist
        if first_vel_val == 1
            cor_t_results(1,4) = ([(1/cor_t_results(1,2))+str_s_results(end,4)]);
            first_vel_val = 0;
        else
            cor_t_results(counter,4) = ([(1/cor_t_results(counter,2))+cor_t_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_s_results(end,2),1) <=  round(cor_t_results(1,2),1)
    brake_begin_str_s = 0;
    cor_t_results(1:track_cor_t_dist,2) = str_s_results(end,2);
    cor_t_results(1:track_cor_t_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_t_dist
        if first_vel_val == 1
            cor_t_results(1,4) = ([(1/cor_t_results(1,2))+str_s_results(end,4)]);
            first_vel_val = 0;
        else
            cor_t_results(counter,4) = ([(1/cor_t_results(counter,2))+cor_t_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector S & T
str_s_brake_dist = track_str_s_dist-brake_begin_str_s;
str_s_brake_perc = (str_s_brake_dist/track_str_s_dist)*100;
str_s_avg_vel = mean(str_s_results); str_s_avg_vel = str_s_avg_vel(1,2);
fprintf('<strong>SIM SECTOR S</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_s_results(end,4)-cor_r_results(end,4), str_s_brake_dist, str_s_brake_perc, str_s_avg_vel, min(str_s_results(:,3)), max(str_s_results(:,3)))
if round(str_s_results(end,2)) < floor(desired_cor_t_vel)
    fprintf(2, '!!!SIM SECTOR T - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_t_vel, str_s_results(end,2))
end
fprintf('<strong>SIM SECTOR T</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_t_results(end,4)-str_s_results(end,4), cor_t_results(1,2))

%>Sector U STR
init_val = 1;
dist_covered = cor_t_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_u_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_t_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_t_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_u_results(tmp_row_counter,1) = ([dist_covered]);
   str_u_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_u_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_u_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_t_results(end,4)]);
   else
       str_u_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_u_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%>Sector V COR
 idx = 1;
 for i = 1:track_cor_v_dist
   cor_v_results(idx,1) = dist_covered;
   idx = idx+1;
   dist_covered = dist_covered+1;
 end
cor_v_results(1:track_cor_v_dist,2) = sqrt(track_cor_v_rad*9.81*tand(cor_v_lean)); 
desired_cor_v_vel = cor_v_results(1,2);
cor_v_results(1:track_cor_v_dist,3) = 0;
if str_u_results(end,2) > (cor_v_results(1,2))       
    [idx_size ~] = size(str_u_results);
    idx = idx_size;
    tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_u_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
    tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
    str_u_results(end,2) = sqrt(str_u_results(idx-1,2)^2+2*tmp_deaccel);
    str_u_results(end,3) = tmp_deaccel;
    str_u_results(end,4) = (1/str_u_results(end,2))+str_u_results(end-1,4);
    while round(str_u_results(end,2),1) >  round(cor_v_results(1,2),1)
        idx = idx-1;
        tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_u_results(idx-1,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
        tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
        str_u_results(idx,2) = sqrt(str_u_results(idx-1,2)^2+2*tmp_deaccel);
        str_u_results(idx,3) = tmp_deaccel;
        str_u_results(idx,4) = (1/str_u_results(idx,2))+str_u_results(idx-1,4);
        brake_begin_str_u =  idx;
        idx_update = idx;
        for i = 1:idx_size-idx
             tmp_idx_vel = find(round(combined_deaccel_limit(:,2),1) == round(str_u_results(idx_update,2),1)); tmp_idx_vel = tmp_idx_vel(1,1);
             tmp_deaccel = combined_deaccel_limit(tmp_idx_vel,1);
             idx_update = idx_update+1;
             str_u_results(idx_update,2) = sqrt(str_u_results(idx_update-1,2)^2+2*tmp_deaccel); 
             str_u_results(idx_update,3) = tmp_deaccel;
             str_u_results(idx_update,4) = (1/str_u_results(idx_update,2))+str_u_results(idx_update-1,4);
             
        end
    end
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_v_dist
        if first_vel_val == 1
            cor_v_results(1,4) = ([(1/cor_v_results(1,2))+str_u_results(end,4)]);
            first_vel_val = 0;
        else
            cor_v_results(counter,4) = ([(1/cor_v_results(counter,2))+cor_v_results(counter-1,4)]);
            counter = counter+1;
        end
    end
elseif round(str_u_results(end,2),1) <=  round(cor_v_results(1,2),1)
    brake_begin_str_u = 0;
    cor_v_results(1:track_cor_v_dist,2) = str_u_results(end,2);
    cor_v_results(1:track_cor_v_dist,3) = 0;
    
    first_vel_val = 1;
    counter = 2;
    for i = 1:track_cor_v_dist
        if first_vel_val == 1
            cor_v_results(1,4) = ([(1/cor_v_results(1,2))+str_u_results(end,4)]);
            first_vel_val = 0;
        else
            cor_v_results(counter,4) = ([(1/cor_v_results(counter,2))+cor_v_results(counter-1,4)]);
            counter = counter+1;
        end
    end
    
end

%Disp stats - Sector U & V
str_u_brake_dist = track_str_u_dist-brake_begin_str_u;
str_u_brake_perc = (str_u_brake_dist/track_str_u_dist)*100;
str_u_avg_vel = mean(str_u_results); str_u_avg_vel = str_u_avg_vel(1,2);
fprintf('<strong>SIM SECTOR U</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_u_results(end,4)-cor_t_results(end,4), str_u_brake_dist, str_u_brake_perc, str_u_avg_vel, min(str_u_results(:,3)), max(str_u_results(:,3)))
if round(str_u_results(end,2)) < floor(desired_cor_v_vel)
    fprintf(2, '!!!SIM SECTOR V - max possible corner speed of %.1fm/s not reached\nAssuming %.1fm/s through the corner instead!!!\n', desired_cor_v_vel, str_u_results(end,2))
end
fprintf('<strong>SIM SECTOR V</strong> - Time: %.1fs, Avg velocity: %.1fm/s\n', cor_v_results(end,4)-str_u_results(end,4), cor_v_results(1,2))

%>Sector W STR
init_val = 1;
dist_covered = cor_v_results(end,1)+1;
tmp_row_counter = 1;
for i = 1:track_str_w_dist
   if init_val == 1
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(cor_v_results(end,2))); tmp_idx_vel = tmp_idx_vel(1,1);
   else
       tmp_idx_vel = find(round(combined_accel_limit(:,2),1) == round(vel_1m_on,1)); tmp_idx_vel = tmp_idx_vel(1,1);
   end 
   tmp_accel = combined_accel_limit(tmp_idx_vel,1);
   
   if init_val == 1
       vel_1m_on = sqrt(cor_v_results(end,2)^2+2*tmp_accel*1);
   else
       vel_1m_on = sqrt(vel_1m_on^2+2*tmp_accel*1);
   end
   str_w_results(tmp_row_counter,1) = ([dist_covered]);
   str_w_results(tmp_row_counter,2) = ([vel_1m_on]);
   str_w_results(tmp_row_counter,3) = ([tmp_accel]);
   if init_val == 1
       str_w_results(tmp_row_counter,4) = ([(1/vel_1m_on)+cor_v_results(end,4)]);
   else
       str_w_results(tmp_row_counter,4) = ([(1/vel_1m_on)+str_w_results(tmp_row_counter-1,4)]);
   end
   dist_covered = dist_covered+1;
   tmp_row_counter = tmp_row_counter+1;
   init_val = 0; 
end

%Disp stats - Sector W
str_w_brake_dist = str_w_results(end,1)-cor_v_results(end,1);
str_w_brake_perc = 100;
str_w_avg_vel = mean(str_w_results); str_w_avg_vel = str_w_avg_vel(1,2);
fprintf('<strong>SIM SECTOR W</strong> - Time: %.1fs, On throttle: %.1fm (%.1f percent), Avg velocity: %.1fm/s, Min & max accel: %.1f/%.1fms^-2\n', str_w_results(end,4)-cor_v_results(end,4), str_w_brake_dist, str_w_brake_perc, str_w_avg_vel, min(str_w_results(:,3)), max(str_w_results(:,3)))

%Combine sim data into one variable
tmp_sim_all_row_1 = [str_a_results(:,1); cor_b_results(:,1); str_c_results(:,1); cor_d_results(:,1); str_e_results(:,1); cor_f_results(:,1); str_g_results(:,1); cor_h_results(:,1); str_i_results(:,1); cor_j_results(:,1); str_k_results(:,1); cor_l_results(:,1); str_m_results(:,1); cor_n_results(:,1); str_o_results(:,1); cor_p_results(:,1); str_q_results(:,1); cor_r_results(:,1); str_s_results(:,1); cor_t_results(:,1); str_u_results(:,1); cor_v_results(:,1); str_w_results(:,1)];
tmp_sim_all_row_2 = [str_a_results(:,2); cor_b_results(:,2); str_c_results(:,2); cor_d_results(:,2); str_e_results(:,2); cor_f_results(:,2); str_g_results(:,2); cor_h_results(:,2); str_i_results(:,2); cor_j_results(:,2); str_k_results(:,2); cor_l_results(:,2); str_m_results(:,2); cor_n_results(:,2); str_o_results(:,2); cor_p_results(:,2); str_q_results(:,2); cor_r_results(:,2); str_s_results(:,2); cor_t_results(:,2); str_u_results(:,2); cor_v_results(:,2); str_w_results(:,2)];
tmp_sim_all_row_3 = [str_a_results(:,3); cor_b_results(:,3); str_c_results(:,3); cor_d_results(:,3); str_e_results(:,3); cor_f_results(:,3); str_g_results(:,3); cor_h_results(:,3); str_i_results(:,3); cor_j_results(:,3); str_k_results(:,3); cor_l_results(:,3); str_m_results(:,3); cor_n_results(:,3); str_o_results(:,3); cor_p_results(:,3); str_q_results(:,3); cor_r_results(:,3); str_s_results(:,3); cor_t_results(:,3); str_u_results(:,3); cor_v_results(:,3); str_w_results(:,3)];
tmp_sim_all_row_4 = [str_a_results(:,4); cor_b_results(:,4); str_c_results(:,4); cor_d_results(:,4); str_e_results(:,4); cor_f_results(:,4); str_g_results(:,4); cor_h_results(:,4); str_i_results(:,4); cor_j_results(:,4); str_k_results(:,4); cor_l_results(:,4); str_m_results(:,4); cor_n_results(:,4); str_o_results(:,4); cor_p_results(:,4); str_q_results(:,4); cor_r_results(:,4); str_s_results(:,4); cor_t_results(:,4); str_u_results(:,4); cor_v_results(:,4); str_w_results(:,4)];
sim_all = [tmp_sim_all_row_1 tmp_sim_all_row_2 tmp_sim_all_row_3 tmp_sim_all_row_4];

%final time print
sim_time = datestr(seconds(sim_all(end,4)), 'MM:SS:FFF');
if compare_sim_vs_real == 1
    real_time = datestr(seconds(real_all(end,4)), 'MM:SS:FFF');
end
fprintf(['<strong>SIM total lap time:</strong> ' sim_time])
if compare_sim_vs_real == 1
    fprintf(['\n<strong>Real total lap time:</strong> ' real_time])
end

%final sim window/plots
call_fig = figure(fig_lapsim);
call_fig.WindowState = 'maximized';
subplot(2,2,1)
plot(sim_all(:,1), sim_all(:,2))
hold on;
if compare_sim_vs_real == 1
    plot(real_all(:,1), real_all(:,2))
end
ylabel('Velocity m/s');
xlabel('Distance m');
title('V(s)')
grid on;
legend('Simulated', 'Real data')
subplot(2,2,2)
plot(sim_all(:,4), sim_all(:,2))
hold on;
if compare_sim_vs_real == 1
    plot(real_all(:,4), real_all(:,2))
end
ylabel('Velocity m/s')
xlabel('Time s')
title('V(t)')
grid on;
if compare_sim_vs_real == 1
    legend(['Simulated ' sim_time], ['Real data ' real_time])
else
    legend(['Simulated ' sim_time])
end
subplot(2,2,3)
plot(sim_all(:,1), sim_all(:,3))
hold on;
if compare_sim_vs_real == 1
    plot(real_all(:,1), real_all(:,3))
end
hold on;
[size ~] = size(sim_all);
tmp_y_axis = zeros(size,1);
plot(sim_all(:,1), tmp_y_axis(:,1),'k--');
ylabel('Acceleration m/s^2')
xlabel('Distance m')
title('Longitudinal acceleration')
grid on;
if compare_sim_vs_real == 1
    legend('Simulated', 'Real data')
else
    legend('Simulated')
end
subplot(2,2,4)
map_image = imread('matlab_donnington_park_gp_map.jpg'); %load map image
imshow(map_image); %display map image
annotation('textbox', [0.65, 0.11, 0, 0], 'string', 'LAP SIMULATOR', 'FitBoxToText', 'on')
hold off;

%code to work on:

%  if combined_accel_limit(end,1) ~= 0
%      combined_accel_limit(end,1) = 0
%  end
%  %^^^needs futher adjustment
%%%the above code is to make sure sim doesnt go out of bounds on
%%%acceleration if the bikes wants to accelerate past current gearing,
%%%not required for 1199s
%------------------------------------------------------------------
%%ACCELERATION THROUGH CORNER CODE NOT FINISHED:
%     idx = 1
%     required_vel = round(cor_b_results(1,2),1)
%     init_val = 1
%     for i = 1:track_cor_b_dist
%         if round(str_a_results(end,2),1) <  required_vel
%             if init_val = 1
%                 cor_b_results(idx,2) = str_a_results(end,2)
%                 cor_b_results(idx,4) = 
%             init_val = 0
%             idx = idx+1
%         else
%             cor_b_results(idx,2) = sqrt(tand(cor_b_lean)*track_cor_b_rad*9.81);
%             cor_b_results(idx,3) = 0;
%             if init_val = 1
%                 cor_b_results(idx,4) = (1/cor_b_results(idx,2))+str_a_results(end,4)
%                 init_val = 0
%             else
%                 cor_b_results(idx,4) = 1/cor_b_results(idx,2)
%             end
%             idx = idx+1
%         end
%     end