forked from K-a-y-D-e-e/newton_law_of_cooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewton_reference.cpp
52 lines (37 loc) · 1.54 KB
/
newton_reference.cpp
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
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate the derivative (rate of change) in Newton's Law of Cooling
double coolingDerivative(double temperature, double mediumTemperature, double coolingConstant) {
return -coolingConstant * (temperature - mediumTemperature);
}
// Function to solve Newton's Law of Cooling using the Euler method
void solveCooling(double initialTemperature, double mediumTemperature, double coolingConstant, double timeStep, double totalTime) {
double temperature = initialTemperature;
double time = 0.0;
cout << "Time\t\tTemperature\n";
while (time <= totalTime) {
cout << time << "\t\t" << temperature << "\n";
// Update temperature using Euler method
temperature += coolingDerivative(temperature, mediumTemperature, coolingConstant) * timeStep;
time += timeStep;
}
}
int main() {
// Parameters
double initialTemperature, mediumTemperature, coolingConstant, timeStep, totalTime;
// Get user input
cout << "Enter the initial temperature: ";
cin >> initialTemperature;
cout << "Enter the medium temperature: ";
cin >> mediumTemperature;
cout << "Enter the cooling constant: ";
cin >> coolingConstant;
cout << "Enter the time step for Euler method: ";
cin >> timeStep;
cout << "Enter the total time for simulation: ";
cin >> totalTime;
// Solve Newton's Law of Cooling and print results
solveCooling(initialTemperature, mediumTemperature, coolingConstant, timeStep, totalTime);
return 0;
}