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 |
#include <iostream> #include <cmath> #include <iomanip> class NewtonsLawOfCoolingSimulator { private: double initialTemperature; // Initial temperature of the object double ambientTemperature; // Ambient temperature double coolingConstant; // Cooling constant (k) double timeStep; // Time step for the simulation public: // Constructor NewtonsLawOfCoolingSimulator(double initialTemperature, double ambientTemperature, double coolingConstant, double timeStep) : initialTemperature(initialTemperature), ambientTemperature(ambientTemperature), coolingConstant(coolingConstant), timeStep(timeStep) {} // Function to simulate cooling over time void simulate(double totalTime) const { int steps = static_cast<int>(totalTime / timeStep); double currentTemperature = initialTemperature; std::cout << "Time\tTemperature\n"; std::cout << "-----------------------\n"; for (int i = 0; i <= steps; ++i) { double time = i * timeStep; currentTemperature = ambientTemperature + (initialTemperature - ambientTemperature) * exp(-coolingConstant * time); std::cout << std::fixed << std::setprecision(2) << time << "\t" << currentTemperature << "\n"; } } }; int main() { double initialTemperature, ambientTemperature, coolingConstant, timeStep, totalTime; // Input parameters from the user std::cout << "Enter the initial temperature of the object: "; std::cin >> initialTemperature; std::cout << "Enter the ambient temperature: "; std::cin >> ambientTemperature; std::cout << "Enter the cooling constant (k): "; std::cin >> coolingConstant; std::cout << "Enter the time step for the simulation: "; std::cin >> timeStep; std::cout << "Enter the total time for the simulation: "; std::cin >> totalTime; // Create a NewtonsLawOfCoolingSimulator object NewtonsLawOfCoolingSimulator simulator(initialTemperature, ambientTemperature, coolingConstant, timeStep); // Run the simulation simulator.simulate(totalTime); return 0; } |
Explanation
- Class Definition (
NewtonsLawOfCoolingSimulator
):- Private Members:
initialTemperature
: Initial temperature of the object.ambientTemperature
: Temperature of the surrounding environment.coolingConstant
: Cooling constant (k), which determines the rate of cooling.timeStep
: Interval of time between calculations in the simulation.
- Private Members:
- Constructor:
- Initializes the simulation parameters with the provided values.
simulate
Method:- Computes and prints the temperature of the object over time.
- Uses the formula T(t)=Ta+(T0−Ta)⋅e−ktT(t) = T_a + (T_0 – T_a) \cdot e^{-kt}, where T(t)T(t) is the temperature at time tt, T0T_0 is the initial temperature, TaT_a is the ambient temperature, and kk is the cooling constant.
- Iterates through each time step, updating and displaying the object’s temperature.
main
Function:- Prompts the user to input values for the initial temperature, ambient temperature, cooling constant, time step, and total time.
- Creates an instance of
NewtonsLawOfCoolingSimulator
with the user’s parameters. - Calls the
simulate
method to output the temperature simulation results over the specified total time.