tags:

views:

46

answers:

2

Let's say we have an empty bathtub. We've lost the plug, so once water is added it will drain away at a constant rate of 2 liters pr. minute. We add water to the tub in increments. 60 liters at 10:51, 30 liters at 11:54 and 50 liters at 13:18.

So, the question is: How can I find out how much water is in the bathtub at any given time?

+1  A: 
water_in_tub(t) = if (t<10:51) then
    0  
else if (10:51<t<11:54) then
    min(60-2*(minutes since 10:51),0)
and so forth

And of course, as I'm sure others will point out, your model of water flowing out of a bath is inaccurate, Toricelli's Law is much more accurate.

High Performance Mark
I told you I suck at math :).The additions of water are dynamic though (an array containing objects with time added and amount), so I can't use the actual numbers in code. ALthough that piece of code might guide me in the right directions for finding an algorithm, thanks :).(the mention of Torricelli's law also gave me plenty of Google food)
Hugi
A: 

Assuming you're modelling a continuous process...

var waterIn = Vbath
var startTime = now()

procedure add_water(var liters) {
  waterIn = how_much_water();
  waterIn = waterIn + liters
  startTime = now()
}

function how_much_water() {
  var waterNow = waterIn - (now() - startTime) * leakSpeed
  if waterNow < 0 return 0 else return waterNow
}
bobah
Thanks - but I forgot to mention that I have to take into account that the tub might empty out between fillings (meaning that for periods of time, no drainage occurs).
Hugi
@Hugi - yes, right, I corrected the answer to support this as well
bobah