Provide Python code to simulate a basic ecosystem predator-prey model.
- User
Provide Python code to simulate a basic ecosystem predator-prey model.
- Assistant
Developing predator-prey model code
The user is asking for Python code to simulate a basic predator-prey ecosystem model, but it's unclear whether they prefer the Lotka-Volterra differential equations or an agent-based approach. Typically, "basic ecosystem predator-prey model" suggests Lotka-Volterra. Although they didn't ask for explanations, I should still provide a concise code snippet along with minimal commentary. I'll create a script with parameters, implement the Euler method for integration, and include a plot of populations over time. Including comments in the code will be useful too.Creating predator-prey simulation code
I’m working on Python code to simulate a predator-prey ecosystem model in a simple and self-contained manner. I should include the Lotka-Volterra equations and use the Euler method for integration while relying only on NumPy and Matplotlib. I'll set parameters like prey and predator populations as well as time settings. In the simulation loop, I’ll implement the differential equations and ensure I clamp negative values, so they don’t drop below zero. I’ll keep it concise, focusing on just one script without extra extensions or animations. ```python """ Basic predator-prey (Lotka–Volterra) ecosystem simulation in Python.
- Prey population: x(t)
- Predator population: y(t)
dx/dt = alphax - betaxy dy/dt = deltaxy - gammay
Run this script to see:
- Population vs. time
- Phase plot (prey vs. predator) """
import numpy as np import matplotlib.pyplot as plt
def predator_prey_simulation( x0=40, y0=9, # initial populations: prey, predators alpha=0.6, # prey growth rate beta=0.025, # rate at which predators eat prey delta=0.01, # predator growth rate per eaten prey gamma=0.5, # predator death rate t_end=60, dt=0.01 # total time and time step ): # Time array t = np.arange(0, t_end, dt) n_steps = len(t)
# Arrays to store populations over time x = np.zeros(n_steps) # prey y = np.zeros(n_steps) # predators # Initial conditions x[0] = x0 y[0] = y0 # Euler integration of the ODEs for i in range(1, n_steps): dx_dt = alpha * x[i-1] - beta * x[i-1] * y[i-1] dy_dt = delta * x[i-1] * y[i-1] - gamma * y[i-1] x[i] = x[i-1] + dx_dt * dt y[i] = y[i-1] + dy_dt * dt # Prevent negative populations due to numerical errors if x[i] < 0: x[i] = 0 if y[i] < 0: y[i] = 0 return t, x, yif name == "main": # Run the simulation t, prey, predators = predator_prey_simulation()
# Plot populations over time plt.figure(figsize=(10, 4)) plt.plot(t, prey, label="Prey", color="tab:green") plt.plot(t, predators, label="Predators", color="tab:red") plt.xlabel("Time") plt.ylabel("Population") plt.title("Predator–Prey Dynamics (Lotka–Volterra Model)") plt.legend() plt.grid(True) plt.tight_layout() # Phase plot (prey vs. predator) plt.figure(figsize=(5, 4)) plt.plot(prey, predators, color="tab:blue") plt.xlabel("Prey population") plt.ylabel("Predator population") plt.title("Phase Plot: Prey vs. Predators") plt.grid(True) plt.tight_layout() plt.show()You can tweak the parameters in `predator_prey_simulation(...)` to explore different ecosystem behaviors.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.