Newton's law of cooling says a hot object loses heat at a rate proportional to its temperature difference from the surroundings. It is a differential equation with a neat solution, and a good place to compare a numerical method with the exact answer. (I build PractiSci , which has a free Newton's law of cooling lab.)
The exact solution
dT/dt = −k (T − Tₛ) gives T(t) = Tₛ + (T₀ − Tₛ) e^(−kt).
const Ts = 25 , T0 = 90 , k = 0.03 ; // °C, °C, per minute const T = ( t ) => Ts + ( T0 - Ts ) * Math . exp ( - k * t ) ; console . log ( T ( 30 ) . toFixed ( 2 ) , T ( 60 ) . toFixed ( 2 ) ) ; // 51.43 35.74
Enter fullscreen mode
Exit fullscreen mode
A cup at 90 °C in a 25 °C room is at 51.43 °C after 30 minutes and 35.74 °C after an hour. The time to reach 40 °C is ln((T₀ − Tₛ)/(40 − Tₛ)) / k:
console . log ( ( Math . log ( ( T0 - Ts ) / ( 40 - Ts ) ) / k ) .…