Alcohol content in blood

Englund

TS Contributor
#1
I'm trying to code a programme to determine the maximum alcohol content in the blood during an evening of drinking as well as the time until one is sober. I think I got the hours until sober correct. But the alcohol content seem to be too high by a factor of ten but I can't see where i'm failing. Any help is appreciated. See code below.

Code:
Alcohol <- function(m,L5,L40,h) {
  Promille <- numeric(h*60)
  t <- c(1:(h*60))
  d <- 0.789; #Alcohol density
  b <- 0.077*m; #Expected blood volume
  bm <- b*1.06*1000; #Expected blood mass (in grams)
  br <- 0.1*m; #Expected alcohol burn rate (in grams)
  gAlc <- (L5*d*0.05+L40*d*0.40)*1000; #Amount of alcohol consumed (in grams)
  Hs <- gAlc/br; #Hours from first drink until sober
  for (i in 1:(h*60)) {
    g <- (gAlc/(h*60))*i; bu <- (br/60)*i
    Promille[i] <- ((g - bu)/bm)*1000
  }
  plot(t,Promille,"l")
  Hs
}

Alcohol(92,3,0,4)
-The body is assumed to begin burning the alcohol from t0.
-The alcohol consumption is assumed to be evenly distributed.
 

Dason

Ambassador to the humans
#2
Add some documentation for us. I'm not you so I don't know what m, L5, L40, h stand for. Short variable names are convenient for coding but make reading the code with no knowledge coming into it a little difficult.
 

Outlier

TS Contributor
#3
Troubleshooting 101:

Use the binary search [chop?] routine.
Halfway through the code, put in a print statement displaying all variable values.
If the fault is upstream, go halfway upstream with another print statement. If downstream, . . .


Yes, use variable names that are clear, and will still be clear to you six months from now.

BTW, I didn't know real pain until I took programming courses. :D
 

Dason

Ambassador to the humans
#4
Yes, use variable names that are clear, and will still be clear to you six months from now.
Yup. When I said "short variable names are convenient for coding but make reading the code with no knowledge coming into it a little difficult" keep in mind that at some point in the future you're going to look at this code again and not remember what everything is. The more self-documenting you can make it the better of you're be.
 

Englund

TS Contributor
#7
My guess is that 'm' stands for mass but this is why some documentation would be nice.
Correct!

m: body weight or mass in kilograms
L5: liters of drinks/beers with an alcohol content of 5 % (weight, not volume)
L40: liters of liquor with an alcohol content of 40 % (weight, not volume)
h: Hours of drinking

As for the for loop: I'm only calculating the alcohol in promille each minute from t0 until no more drinks are consumed for the evening. I.e grams of alcohol consumed up to, say, t_120 (120 minutes after first drink). The following formula is the amount of alcohol consumed minus alcohol burned, divided by blood mass (everything in grams), times thousand is there in order get it in promille:

((g - bu)/bm)*1000

And here it gets wrong somehow. It seem to be too high by a factor of ten, but I can't figure out why.
 

Outlier

TS Contributor
#8
It may be a Dimensional Analysis problem and not a programming problem.

Try to convert miles/usec into furlongs/fortnight for an extreme example of what I mean.

And, thanks for introducing me to "promille".