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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include <iostream> #include <vector> #include <iomanip> class PredatorPreyModel { public: PredatorPreyModel(double preyInitial, double predatorInitial, double preyBirthRate, double predatorDeathRate, double predationRate, double reproductionRate, double timeStep) : prey(preyInitial), predator(predatorInitial), preyBirthRate(preyBirthRate), predatorDeathRate(predatorDeathRate), predationRate(predationRate), reproductionRate(reproductionRate), timeStep(timeStep) {} void simulate(double totalTime) { std::cout << std::fixed << std::setprecision(2); std::cout << "Time\tPrey\tPredators\n"; for (double t = 0; t <= totalTime; t += timeStep) { // Output current state std::cout << t << "\t" << prey << "\t" << predator << "\n"; // Update populations double preyChange = preyBirthRate * prey - predationRate * prey * predator; double predatorChange = reproductionRate * prey * predator - predatorDeathRate * predator; prey += preyChange * timeStep; predator += predatorChange * timeStep; } } private: double prey; double predator; double preyBirthRate; double predatorDeathRate; double predationRate; double reproductionRate; double timeStep; }; int main() { double preyInitial, predatorInitial; double preyBirthRate, predatorDeathRate; double predationRate, reproductionRate; double timeStep, totalTime; std::cout << "Enter initial prey population: "; std::cin >> preyInitial; std::cout << "Enter initial predator population: "; std::cin >> predatorInitial; std::cout << "Enter prey birth rate: "; std::cin >> preyBirthRate; std::cout << "Enter predator death rate: "; std::cin >> predatorDeathRate; std::cout << "Enter predation rate: "; std::cin >> predationRate; std::cout << "Enter reproduction rate: "; std::cin >> reproductionRate; std::cout << "Enter time step: "; std::cin >> timeStep; std::cout << "Enter total simulation time: "; std::cin >> totalTime; PredatorPreyModel model(preyInitial, predatorInitial, preyBirthRate, predatorDeathRate, predationRate, reproductionRate, timeStep); model.simulate(totalTime); return 0; } |
Explanation
- Class
PredatorPreyModel
:- Purpose: Simulates the dynamics of predator and prey populations using the Lotka-Volterra equations.
- Attributes:
prey
: Current population of prey.predator
: Current population of predators.preyBirthRate
: Birth rate of prey.predatorDeathRate
: Death rate of predators.predationRate
: Rate at which predators kill prey.reproductionRate
: Rate at which predators reproduce based on prey.timeStep
: Time increment for each simulation step.
- Methods:
simulate(double totalTime)
: Runs the simulation for the specified total time.- Updates: Calculates changes in prey and predator populations based on the Lotka-Volterra equations.
- Outputs: Prints the time, prey population, and predator population at each time step.
- Main Function:
- Setup: Prompts the user to input initial populations, rates, time step, and total simulation time.
- Simulation: Creates an instance of
PredatorPreyModel
and runs the simulation.
Usage
- Modeling Ecosystems: Demonstrates how predator and prey populations interact over time.
- Dynamic Behavior: Provides insights into the cyclical nature of predator-prey relationships.