ESA 2026 Theory Workshop

For-Loop Exercise

Complete the missing pieces of the code below using the following description of a discrete logistic model. You can run the simulation and check your answers.

The discrete logistic growth model is: Nt+1 = Nt + rNt(1 − Nt/K).
At each time step, the population changes according to a density-dependent per-capita growth rate. Here, r is the intrinsic growth rate for the continuous exponential growth (i.e. r + 1 = &lambda in the discrete exponential growth model), while (1 − N/K) reduces that growth rate as the population approaches the carrying capacity K.

Simulation Parameters

Initialize the parameters below for running the simulation. The simulation uses your values, even though the example code below displays default values for demonstration.

N0
r
K
end_time
# Step 1. Initialize the model parameters outside the for-loop. N0 <- 10 r <- 0.2 K <- 100 end_time <- 100 time_step <- 1 # Step 2. Initialize the vector that stores the population through time. pop_vec <- numeric(end_time) pop_vec[1] <- N0

What is a vector? A vector is an ordered list of values. In this example, the first value stores the initial population size; the second value stores the population after one time step; the third value stores the population after two time steps, and so on. By storing the population size at every time step, we can later visualize the entire population trajectory.

for (t in seq(0, end_time, by = time_step)) { # Step 3. Extract the current population size to prepare for Step 4. N_t <- # Step 4. Calculate the next population size according to the model/biological process. N_tplus1 <- # Step 5. Store the updated population size. pop_vec[t + 1] <- # Step 6. Iterate to the next time step and repeat the process in the for-loop until the end time is reached. }

Tips: