function [ reinoculation, who  ] = find_time_when_best_is_percent_better( t, insert_od, start_values, growth_rates, percent )
%tries to find out when the best is ... percent better then the other
%   t: time for reinoculation
%   insert_od: the od from all species together
%   start_values: are the start od from every species
%   growth_rates: are all grwoth_rates for every species
%   percent: how much on species is better

%when we say that no cell gets reinoculation
die_od=0.0001;

%reinoculation counter
reinoculation=0;

%percent the other of the whole od
percent_difference=0;

%number oof species to compare
numberOfSpecies=length(growth_rates);

%differnt colors
cc=hsv(numberOfSpecies);

startValuesForGrowing=[];

%sort the start values for the integration
for i=0:numberOfSpecies-1
        startValuesForGrowing(2*i+1)= growth_rates(i+1); 
        startValuesForGrowing(2*i+2)=start_values(i+1);
end


%find when the best is ... percent more there than the other
while percent_difference< percent
    
    %in which figure to plot an all plots behind goes in one figure
    f=figure(reinoculation+1);
    hold on;
    
    %it only works with nonstifft ode solver
    % [T,X]= ode45(@competitive_verhulst_dynamical_r, [0 tn], [rx x0 ry y0], [] , 0.35);
    % plot(T,X);
    %         legend('rX','x','ry','y');
    %solve differntial equation
    [T,X]= ode45(@competitive_verhulst_dynamical_r, [0 t], startValuesForGrowing, [] , 0.35);
    
    %all end concentration
    x=[];
    
    %plot all growth curves and remember the end concentration
    for j=1:numberOfSpecies
        speciesAmount=X(:,j*2);
        x(j)=speciesAmount(end);
        plot(T,speciesAmount, 'color',cc(j,:));
    end
    
%     %defined the axes
    xlabel('time (h)', 'FontSize', 15) ;
    ylabel('OD', 'FontSize', 15) ;
legend('optimal affinity','moderate affinity','bad affinity');
    %test=legend('100% affinity', '95% affinity', '40% affinity');
    
    %save figure
    print(['-f' int2str(reinoculation+1)],['competitom_' int2str(t) 'Time' int2str(percent) 'Percent' int2str(reinoculation)],'-dpng');
    
    %close figure
    close(f);
    
    %is the whole OD of all species together 
    whole_Od=sum(x);
    
    %definde the percent difference
    percent_difference=0;
    
    %how much percent are the other species (not the best)
    percent_other=0;
    
    %calculate the new OD start values 
    for i=1:numberOfSpecies
        
        %proportion of the species
        procX= x(i)/whole_Od;
%         display(procX);
        
        %remeber how much has the first one and how much the other s
        if i ~=1
            percent_other=percent_other+ procX*100;
        else
            percent_best=procX*100;
        end
        
        %calucate the new start value
        if (x(i)~= 0)
            %new start value th stoachstic
            %s0=insert_od*procX;
            s0=binornd(fix(insert_od*8*10^8),procX);
            if s0 > die_od
                startValuesForGrowing(2*i)=s0/(8*10^8);
            else
                startValuesForGrowing(2*i)=0;
            end
           
        else
           startValuesForGrowing(2*i)=0;
        end
        
        
        
        
    end
    
    %calculate the difference
    percent_difference=percent_best-percent_other;
%     display(percent_other);
        
%     display(percent_best);
%     display(percent_difference);
%     display(percent);
    reinoculation=reinoculation+1;
    
    
end
%because the last step bevor the loop break was to add 1 to this parameter 
reinoculation = reinoculation-1;

%sort the growth rate out
onlyOD=[];
for k=1:numberOfSpecies
   onlyOD(i)=startValuesForGrowing(i*2); 
end

%get the one who won
who=find(onlyOD==max(onlyOD));




end

