Skip to content

Instantly share code, notes, and snippets.

@escuccim
Last active November 10, 2018 16:43
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 escuccim/323a56730385be6b9b14b546c8c2eb56 to your computer and use it in GitHub Desktop.
Save escuccim/323a56730385be6b9b14b546c8c2eb56 to your computer and use it in GitHub Desktop.
Python function to calculate blood levels of medication
# dose: daily dose
# halflife: half-life in hours
# start_level: initial blood levels
# days: days to model
def blood_level(dose, halflife, start_level=0, days=200):
# starting level = 0
level_0 = start_level
# level after first dose = dose
level = dose + start_level
# hf in days
hf_days = halflife / 24
# start at day 0
day = 0
levels = [level_0]
# loop through days
while day < days:
# how much will the levels decrease today
day_decrease = (level / 2) / hf_days
# subtract the decrease
level -= day_decrease
# add the daily dose
level += dose
levels.append(level)
# increase the day
day += 1
print("Day:", day, "starting level:", level_0, "end level:", level)
level_0 = level
plt.plot(np.arange(days+1), levels)
plt.xlabel("Days")
plt.ylabel("Blood Level")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment