# prize35-8 Exercise 1: find the prey and predator peaks and the lag between them prey, pred = 40.0, 20.0 a, b, c, d = 0.6, 0.02, 0.01, 0.5 dt = 0.01 preys, preds = [], [] for step in range(6001): preys.append(prey) preds.append(pred) new_prey = prey + (a * prey - b * prey * pred) * dt new_pred = pred + (c * prey * pred - d * pred) * dt prey, pred = new_prey, new_pred def peaks(series): return [round(i * dt, 1) for i in range(1, len(series) - 1) if series[i] > series[i - 1] and series[i] >= series[i + 1]] pp, dp = peaks(preys), peaks(preds) print("prey peaks (year):", pp) print("predator peaks (year):", dp) periods = [round(y - x, 1) for x, y in zip(pp, pp[1:])] print("average prey period:", round(sum(periods) / len(periods), 1), "years") lags = [round(q - p, 1) for p, q in zip(pp, dp)] print("predator peak comes after prey peak by:", lags) # Output: # prey peaks (year): [3.9, 15.6, 27.3, 39.0, 50.7] # predator peaks (year): [6.3, 18.0, 29.6, 41.3, 53.0] # average prey period: 11.7 years # predator peak comes after prey peak by: [2.4, 2.4, 2.3, 2.3, 2.3] # Reading it: the prey peak first, and the predator peak follows about 2.3 to 2.4 years later, because predators need time to turn a feast into more predators. # The whole cycle repeats about every 11.7 years. Predators peak after prey and crash after prey crash, which is the lag that makes this a cycle rather than a steady state.