function [ti_sim kc] = optimaltuning(gp, ms_s, i, ti_sim)

kc = [];

kc0=0;


% Alternative ti ranges, used for some difficult simulations, where the
% process becomes lag dominated

    %if i >= 15
     %   kc0=i/2;
      %  ti_sim = (4:0.1:i+3)';;
         
%     if i == 1;
%         
%         kc0 = 0.5;
%         
%         ti_sim = [4:0.1:13,100,1000,10000,1000000]';
%     elseif i>1 && i<13;
%         kc0=0.5;
%         ti_sim = [5:0.1:16,100,1000,10000,1000000]';
%         
%     elseif i>=13 && i<40
%           kc0=0.5;
%           ti_sim = [4:0.1:16,100,1000,10000,1000000]';
%     elseif i>=40
%           kc0=0.5;
%           ti_sim = [3:0.1:16,100,1000,10000,1000000]';
% 
%     end


% if i < 15
%     kc0=0;
%     ti_sim = [0.01:0.5:0.990,1:1:16]';
% else
%     kc0=i/2;
%     ti_sim = [4:0.01:i+3]';
% end
% 
% 

  for n = 1:size(ti_sim,1)


    ti=ti_sim(n);

    [kc_temp, fval] = fsolve(@(kc) myfun(gp,kc,ti,ms_s),kc0);
    
    kc0=kc_temp;
    
    kc=[kc; kc_temp];
    

  end


end






