Skip to content

Instantly share code, notes, and snippets.

@ryanthejuggler
Created March 25, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanthejuggler/9763822 to your computer and use it in GitHub Desktop.
Save ryanthejuggler/9763822 to your computer and use it in GitHub Desktop.
Blood caffeine concentration estimate
var duration = 24*3600*1000; // 24 hours as milliseconds
var halfLife = 5*3600*1000; // 5 hours as milliseconds
var absorpLife = 3600*1000; // 1 hour as milliseconds
var end = +(new Date());
var start = end - duration;
var numSteps = 24*60;
var step = duration/numSteps;
var startVal = 106;
var rows = [];
var log2 = Math.log(2);
var log95 = Math.log(0.05);
var mymass = 68; // kg
var bloodVolume = 70e-3; // L/kg
function calculateConcentration(events, t, start){
var sum = 0;
var e;
for(var i=0;i<events.length;i++){
e = events[i];
if(e.time > (t+start)) continue;
sum += e.amt * Math.exp(-(t-(e.time-start))*log2/halfLife)
* (1-Math.exp((t-(e.time-start))*log95/absorpLife));
}
return sum/(mymass*bloodVolume);
}
var evts = [
{time: new Date("7/17/2013 1:30 AM"), amt:80},
{time: new Date("7/17/2013 3:00 AM"), amt:80},
{time: new Date("7/16/2013 10:30 PM"), amt:80}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment