Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.