RHS ¼ zeros(Npatch,1); % Set matrix for Right Hand Side of equn
for i ¼ 1: Npatch % Cycle over patches ¼ pack sizes
% Call Fitness function. Pass Benefit and Pbenefit as vectors
RHS(i) ¼ FITNESS(X, Xcritical, Xmax, Xinc, Cost, Benefit(i,:),
Pbenefit(i,:), F_vectors);
end % End of i loop
% Now find optimal patch Sorted_RHS(1)¼Highest RHS, I¼Row¼patch
number
[Sorted_RHS,I] ¼ sort(RHS,‘descend’); % Sorts into descending
col
Index ¼ 1þ(X-Xcritical)/Xinc; % Get Index value
F_vectors(Index,1) ¼ Sorted_RHS(1);
Best_Patch ¼ I(1);
% Concatenate F(x,t,T) and the optimal patch number
Temp ¼ [F_vectors(Index,1), Best_Patch];
% Add Temp to bottom of F.vectors and rename to Temp
Temp ¼ vertcat(F_vectors, Temp);
% Create 1x2 vector to hold decision on more than one choice
% We only need one cell but it is convenient to use 2 for concatena-
tion
% onto Temp, as indicated below
Choice =[0,0];
if Sorted_RHS(1)== Sorted_RHS(2) % Equal fitnesses
Choice ¼ [1,1]; % Equal fitnesses
end
Temp ¼ vertcat(Temp, Choice);
% End of function
% ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ....
% Function to iterate over states of X
function Temp¼OVER_STATES(F_vectors, Xcritical, Xmax, Xinc,
Npatch, Cost, Benefit, Pbenefit, Max_Index)
Store ¼ zeros(Max_Index,3); % Create matrix for output
for Index ¼ 2 : Max_Index % Iterate over states of X
% For given X call Over_Patches to determine F(x,t,T) and best patch
X ¼ (Index-1)*Xinc þ Xcritical;
Temp ¼ OVER_PATCHES(X, F_vectors, Xcritical, Xmax, Xinc,
Npatch, Cost, Benefit, Pbenefit);
% Extract components_ Penultimate row is F(x,t,T) and best patch
n ¼ size(Temp,1)-2;
F_vectors ¼ Temp(1:n,1:2);
Store(Index,1:2) ¼ Temp(nþ1,1:2); % Save F(x,t,T) and best patch
Store(Index,3) ¼ Temp(nþ2,1); % Save flag indicating choices
end % End of X loop
% Add Store values to end of F_vectors for pass back to main program
Temp
¼ horzcat(F_vectors,
Store); % Combined by columns
410 MODELING EVOLUTION