Find current value of a variable that depreciates linearly over time
Let's say we have an empty bathtub. We've lost 开发者_StackOverflow中文版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?
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.
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
}
精彩评论