% Plot fitness¼W vs x for both columns on same graph
plot(x,W(:,1)) % Plot first line
xlabel(’Body size, x’);ylabel(’Fitness, W’);
hold on % Keep plot for next line
plot(x,W(:,2),’:’) % Plot second line with dashes
2.18.14 Scenario 6: Finding the maximum using the calculus
As with the R code there is a function, INTEGRAND, to generate the fitness value
for a given x and a function, FITNESS, that calls the numerical integration routine
dblquad which takes INTEGRAND as its input. Two points are worth noting. First,
we have to pass an extra parameter, x to the integration routine and second we
have to compress the fitness equation given in INTEGRAND into a single line. This
is done by making use of the routines min and max. The survival equation is
AsBs*x, except that it is bounded at 0 and 1. This is specified by nesting the
min and max routines: min (max ((AsBs*x), 0), 1). The inner routine
ensures that survival does not go below 0 and the outer routine ensures that it
does not exceed 1 (we could have used the same code in R but it is not as clear and
makes little difference to the speed of execution).
function f¼INTEGRAND(As,Bs,x) % Function to integrate function
Af ¼2; Bf ¼2; Ca ¼1/0.7; Cb ¼5; % Invariant parameter values
f¼(AfþBf*x)*min(max((AsBs*x),0),1)*Cb*Ca;% Fitness vector
Function to calculate fitness:
function W¼FITNESS(x) % Function to evaluate fitness
Amin ¼ 0.3; Amax ¼ 1; % Min and max values of As
Bmin ¼ 0; Bmax ¼ 0.2; % Min and max values of Bs
% Double integral. Note that x is passed also
W ¼dblquad(@(As,Bs) INTEGRAND(As,Bs,x),Amin,Amax,Bmin,Bmax);
W¼-W; % Negative of fitnessc
Main Program:
clear all; % Clear the workspace
fminsearch(@FITNESS,2) % Find minimum, starting with 2
OUTPUT:
ans ¼ 3.3703
The answer given by MATLAB agrees with that obtained using optimize in R.
2.18.15 Scenario 6: Finding the maximum using a numerical approach
The fitness function uses the same general structure as previously used in INTE-
GRAND:
FISHERIAN OPTIMALITY MODELS 147