the vector of survival probabilities is calculated and values checked that they fall
within the acceptable range (0–1). In addition to calculating mean fitness as a
function of x (body size), the following code also calculate fitness using the mean
parameter values. For each parameter vectors of 1,000 random values are gener-
ated. These vectors are then used in calculating the fitness for a given body size x.
The survival vector Surv is 1,000 units long. For simplicity and without loss of
significant run time a loop is used to iterate over values of body size.
R CODE (Figure 2.6):
rm(list¼ls()) # Remove all objects from memory
Af <-2;Bf<- 2 # Invariant parameter values
Amin <- 0.3; Amax <- 1 # Min and max values of
a
S
Bmin <- 0; Bmax <- 0.2 # Min and max values of
b
S
Amean <- (AmaxþAmin)/2 # Mean value of
a
S
Bmean <- (BmaxþBmin)/2 # Mean value of
b
S
# Calculate n parameter combinations
n <- 1000 # Number of values of a
S
and b
S
to generate
# We are assuming a uniform distribution of values
set.seed(10) # Set the random number seed
# Generate n random numbers from Bmin to Bmax
Bs <- runif(n, min¼Bmin, max¼Bmax)
# Generate n random numbers from Amin to Amax
As <- runif(n, min¼Amin, max¼Amax)
x <- seq(from¼0, to¼6, length¼100) # Bodysizes from 0 to 6
W <- matrix(0,100,2) # Matrix to take fitness values
for ( i in 1: 100) # Iterate over x values
{
Surv <-AsBs*x[i] # Vector of survivals
# Check that no survival < 0. If so then set to zero
Surv[Surv<0] <-0
# Check that no survival > 1. If so then set to 1
Surv[Surv>1] <-1
# Column 1 contains fitness for variable parameters
W[i,1] <- mean((AfþBf*x[i])*Surv)
# Col 2 contains fitness using mean parameter values
W[i,2] <- (AfþBf*x[i])*(Amean-Bmean*x[i])
}
# Plot fitness¼W vs x for both columns on same graph
plot(x,W[,1], xlab¼’Body size, x’, ylab¼’Fitness, W’,las¼1,
lwd¼4) # Dots
lines(x,W[,2], lwd¼4) # Line
MATLAB CODE: See Section 2.18.13.
Notice that the two parameter vectors are outside of the loop over x.Thisisimpor-
tant for two reasons: first, because only one pair of vectors is calculated, it speeds up
96 MODELING EVOLUTION