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 |
#include <iostream> #include <iomanip> // Class representing the Epidemic Model class EpidemicModel { public: EpidemicModel(double initialSusceptible, double initialInfected, double initialRecovered, double infectionRate, double recoveryRate, double timeStep) : susceptible(initialSusceptible), infected(initialInfected), recovered(initialRecovered), infectionRate(infectionRate), recoveryRate(recoveryRate), timeStep(timeStep) {} void simulate(double totalTime) { std::cout << std::fixed << std::setprecision(2); std::cout << "Time\tSusceptible\tInfected\tRecovered\n"; for (double t = 0; t <= totalTime; t += timeStep) { // Output current state std::cout << t << "\t" << susceptible << "\t" << infected << "\t" << recovered << "\n"; // Calculate changes double newInfections = infectionRate * susceptible * infected; double newRecoveries = recoveryRate * infected; // Update states susceptible -= newInfections * timeStep; infected += (newInfections - newRecoveries) * timeStep; recovered += newRecoveries * timeStep; // Ensure values remain within bounds if (susceptible < 0) susceptible = 0; if (infected < 0) infected = 0; if (recovered < 0) recovered = 0; } } private: double susceptible; double infected; double recovered; double infectionRate; double recoveryRate; double timeStep; }; int main() { double initialSusceptible, initialInfected, initialRecovered; double infectionRate, recoveryRate; double timeStep, totalTime; std::cout << "Enter initial number of susceptible individuals: "; std::cin >> initialSusceptible; std::cout << "Enter initial number of infected individuals: "; std::cin >> initialInfected; std::cout << "Enter initial number of recovered individuals: "; std::cin >> initialRecovered; std::cout << "Enter infection rate (per susceptible and infected): "; std::cin >> infectionRate; std::cout << "Enter recovery rate (per infected): "; std::cin >> recoveryRate; std::cout << "Enter time step (in days): "; std::cin >> timeStep; std::cout << "Enter total simulation time (in days): "; std::cin >> totalTime; EpidemicModel model(initialSusceptible, initialInfected, initialRecovered, infectionRate, recoveryRate, timeStep); model.simulate(totalTime); return 0; } |
Explanation
- Class
EpidemicModel
:- Purpose: Simulates the spread of an epidemic over time.
- Attributes:
susceptible
: Number of susceptible individuals.infected
: Number of infected individuals.recovered
: Number of recovered individuals.infectionRate
: Rate at which susceptible individuals get infected by contact with infected individuals.recoveryRate
: Rate at which infected individuals recover.timeStep
: Time interval for each simulation step.
- Methods:
simulate(double totalTime)
: Runs the simulation from time0
tototalTime
.- Calculations: Updates the number of susceptible, infected, and recovered individuals based on the infection and recovery rates.
- Outputs: Prints the state of the epidemic at each time step.
- Main Function:
- Setup: Prompts the user for initial conditions, rates, time step, and total simulation time.
- Simulation: Creates an instance of
EpidemicModel
and runs the simulation.
Usage
- Epidemic Simulation: Provides a basic model to simulate how an epidemic spreads through a population over time.
- User Interaction: Allows users to input parameters and see the progression of the epidemic.