#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;
}