1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <iostream> // Structure to represent the state of the ecosystem struct EcosystemState { double prey_population; // Population of prey double predator_population; // Population of predators }; // Function to update the ecosystem state based on the Lotka-Volterra equations void updateEcosystemState(EcosystemState& state, double alpha, double beta, double delta, double gamma, double dt) { // Compute the changes in population double dPrey = (alpha * state.prey_population - beta * state.prey_population * state.predator_population) * dt; double dPredator = (delta * state.prey_population * state.predator_population - gamma * state.predator_population) * dt; // Update populations state.prey_population += dPrey; state.predator_population += dPredator; } int main() { // Initial conditions EcosystemState ecosystem = {40.0, 9.0}; // Initial populations: 40 prey, 9 predators // Parameters for the Lotka-Volterra model double alpha = 0.1; // Prey birth rate double beta = 0.02; // Predation rate double delta = 0.01; // Predator reproduction rate double gamma = 0.1; // Predator death rate double dt = 0.1; // Time step (in arbitrary time units) int steps = 100; // Number of simulation steps // Simulate the ecosystem dynamics for (int i = 0; i < steps; ++i) { updateEcosystemState(ecosystem, alpha, beta, delta, gamma, dt); // Print the current state of the ecosystem std::cout << "Time: " << i * dt << ", " << "Prey Population: " << ecosystem.prey_population << ", " << "Predator Population: " << ecosystem.predator_population << "\n"; } return 0; } |
Explanation
- EcosystemState Structure:
- This structure holds the state of the ecosystem, including the populations of prey and predators.
- Main Function:
- The ecosystem is initialized with 40 prey and 9 predators.
- The program simulates the ecosystem for 100 steps, with each step representing an arbitrary time unit.
- The state of the ecosystem is printed after each time step, showing the populations of prey and predators.
Usage
- Initial Populations: The initial populations of prey and predators are set to 40 and 9, respectively.
- Simulation Parameters: The parameters α\alpha, β\beta, δ\delta, and γ\gamma control the rates of birth, predation, reproduction, and death, respectively.
- Output: The program outputs the populations of prey and predators at each time step, allowing you to observe how the ecosystem evolves over time.