Ruby math: Newton's Law of Cooling

I had a frozen pipe break in a house I was watching. Although the pipe
is now fixed and insulation was added above the pipes, the PVC (polyvinayl choride)
water lines are still freezing.

So, I decided I will calculate the times in which these
water lines freeze and run over and turn on the water before they freeze.

I want to be able to calculate the time in which these water pipes
will freeze at various cold temperatures.

I was wondering how I would compose the following “Newton’s Law of Cooling” into
a Ruby calculation:

http://scienceworld.wolfram.com/physics/NewtonsLawofCooling.html

t = time
T = changing temp of object
T_s = temp of surrounding environment
T_0 = initial temperature of the object
K = an experimental constant that has to do with water and surface area

T(t) = T_s + (T_0 - T_s)e^(-Kt)

P.S.
I have a BA in a humanities discipline :frowning:

···

Ted Knab
Chester, MD 21619

35570707f6274702478656021626f6c6964796f6e602f66602478656
02e6164796f6e60237471647560216e6460276c6f62616c60257e696
4797e2a0

Newton's Law of Cooling -- from Eric Weisstein's World of Physics

t = time
T = changing temp of object
T_s = temp of surrounding environment
T_0 = initial temperature of the object
K = an experimental constant that has to do with water and surface area

T(t) = T_s + (T_0 - T_s)e^(-Kt)

well, the problem is that K is an experimental constant and must be
computed. To compute it, you must know the value of T at 2 differents
times, say t0 and t1.

This will give you the relation given in (7)

    K = 1/t1 * ln((T0 - Ts) /(T1 -Ts))

then to find the time needed to reach T2, you have the equation (8)

    t2 = t1 * (ln((T0 - Ts)/(T2 - Ts))/ln((T0 - Ts)/(T1 - Ts)))

which translated in ruby is just

    t2 = t1 * (Math::log((T0 - Ts)/(T2 - Ts))/Math::log((T0 - Ts)/(T1 - Ts)))

this give something like this

svg% cat b.rb
#!/usr/bin/ruby
T0 = 12.0 # initial temperature
Ts = -6.0 # surrounding temperature
T1 = 6.0 # temperature after 60s
D1 = 60.0 # time to reach T1

def reach(t)
   D1 * Math::log((T0 - Ts)/(t - Ts)) / Math::log((T0 - Ts)/(T1 - Ts))
end

p reach(6) # time to reach 6 (it must give D1)
p reach(0) # time to reach 0
svg%

svg% b.rb
60.0
162.570677481087
svg%

Guy Decoux

In article 20040201142653.GA26053@annapolislinux.org,

I had a frozen pipe break in a house I was watching. Although the pipe
is now fixed and insulation was added above the pipes, the PVC
(polyvinayl choride)
water lines are still freezing.

So, I decided I will calculate the times in which these
water lines freeze and run over and turn on the water before they freeze.

I want to be able to calculate the time in which these water pipes
will freeze at various cold temperatures.

I was wondering how I would compose the following “Newton’s Law of Cooling” into
a Ruby calculation:

Newton's Law of Cooling -- from Eric Weisstein's World of Physics

t = time
T = changing temp of object
T_s = temp of surrounding environment
T_0 = initial temperature of the object
K = an experimental constant that has to do with water and surface area

T(t) = T_s + (T_0 - T_s)e^(-Kt)

If you just need to know how to transliterate the equation then it’s
relatively simple, keeping in mind the initial case of a ruby identifier
matters, and that we need to use floating point rather than integers.

A simple tramsliteration might be:

t_s = 100.0
t_0 = 1000.0
K = 1.234

temp at t = 0

t = 0.0
puts t_s + (t_0 - t_s) * Math.exp(-K * t)

temp at t = 1.0

t = 1.0
puts t_s + (t_0 - t_s) * Math.exp(-K * t)

And this yeilds

100.0
362.013168336477

That may not be the most convenient thing to use, but you can see how
the formula is converted to ruby. This should answer your question but
not ruin the fun of packaging the formula into a more useful form in
your program.

Hope this helps,

Mike

···

Theodore Knab tjk@annapolislinux.org wrote:

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Hi!

  • Theodore Knab:

I was wondering how I would compose the following “Newton’s Law of
Cooling” into a Ruby calculation:

Newton's Law of Cooling -- from Eric Weisstein's World of Physics

t = time
T = changing temp of object
T_s = temp of surrounding environment
T_0 = initial temperature of the object
K = an experimental constant that has to do with water and surface area

T(t) = T_s + (T_0 - T_s)e^(-Kt)

Perhaps it is helpful to explain the physics behind that formula. It
will be very elementary. What do we know?

First of all we know that we start with some initial temperature of
the object. We call it T_0.

Then we know that if we wait long enough the temperature will become
that of the surrounding environment.

Experiments show that the temperature change is proportional to the
difference between the temperature of the object and the temperature
of the environment and the timespan you wait (given that the
temperature does not change significantly during that timespan) So
for a short timespan dt the following holds:

T(t + dt) - T(t) =  K * [T(t) - T(s)] * dt

Now let us take a look at K. This constant describes how fast the
heat exchange takes place. This depends on several factors (in no
particular order):

  • The surface through which the exchange takes place. If the surface
    is large the echange is faster.
  • The amount of matter that has to be cooled down. Dinosaurs were de
    jur cold-blooded but de facto warm-blooded.
  • The heat capacity of the material that is cooled down.
  • Who likely the energy is transported away (depends on isolation,
    humidity of the environment, etc.)

Anyway. In the limit of dt versus zero the above difference equation
becomes Newtons Law of Cooling. Now let’s come to the problem of how
to use the equation. For this you have to assume that the temperature
is the only enviromental influence that is subject to change.
Otherwise things become very complicated.

The first task is finding out K. On a day where wether is very
constant so that you need not bother about a changing environment
temperature you measure the temperature of the object at fixed time
intervals and write down temperature and time. Now let’s modifiy the
equation:

T(t) = T_s + (T_0 - T_s)e^(-Kt)
T(t) - T_s = (T_0 - T_s) * e^(-K
t)
(T(t) - T_s) / (T_0 - T_s) = e^(-Kt)
ln (T(t) - T_s) - ln( T_0 - T_s) = -K
t
ln (T(t) - T_s) = ln( T_0 - T_s) - K*t

Let’s use the abbreviations x(t) = ln (T(t) - T_s) and
x_0 = ln (T_0 - T_s):

x(t) = x_0 - K * t

This means: If you plot x(t) on the vertical axis and t on the
horizontal axis the slope is -K.

Does that answer the question?

Josef ‘Jupp’ SCHUGT

···


http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Germany 2004: To boldly spy where no GESTAPO / STASI has spied before

Hi

I fear your won’t get very good results with this formula. I only tells you
at which time the water in the pipes will reach 0 degrees celcius (sorry,
don’t know what this is is farenheit). But even after reaching 0 degrees, it
will take quite a time until the water starts to freeze - the water has to
loose a lot of energy to it’s environment to get from the liquid state into
crystaline state.

greetings, Florian Pflug

···

On Sun, Feb 01, 2004 at 11:26:56PM +0900, Theodore Knab wrote:

I had a frozen pipe break in a house I was watching. Although the pipe
is now fixed and insulation was added above the pipes, the PVC (polyvinayl choride)
water lines are still freezing.

So, I decided I will calculate the times in which these
water lines freeze and run over and turn on the water before they freeze.

I want to be able to calculate the time in which these water pipes
will freeze at various cold temperatures.

I was wondering how I would compose the following “Newton’s Law of Cooling” into
a Ruby calculation:

Newton's Law of Cooling -- from Eric Weisstein's World of Physics

t = time
T = changing temp of object
T_s = temp of surrounding environment
T_0 = initial temperature of the object
K = an experimental constant that has to do with water and surface area

T(t) = T_s + (T_0 - T_s)e^(-Kt)

Hi!

  • Florian G. Pflug:

I only tells you at which time the water in the pipes will reach 0
degrees celcius (sorry, don’t know what this is is farenheit).

0 degree Celsius is 0 degree centigrade is 32 degree Fahrenheit is
273.15 Kelvin (sometimes mistermed ‘degree Kelvin’ even though it is
no degree scale)

But even after reaching 0 degrees, it will take quite a time until
the water starts to freeze - the water has to loose a lot of energy
to it’s environment to get from the liquid state into crystaline
state.

To give numbers the specific melting enthalpie of H20 is 334 kJ/kg.
Note that impurities like HCl can change the melting temperature of
water by several Kelvin.

Josef ‘Jupp’ SCHUGT

···


http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Germany 2004: To boldly spy where no GESTAPO / STASI has spied before