3
$\begingroup$

Background of the problem: A patient takes a drug every $X$ hours with a half-life of $Y$ hours. How much of the drug will be (exactly) in his system after $h$ hours? For $X=24$, and $Y=24$ I made the following function showing the concentration of the drug for the first 96 hours.

 Clear[m]
 m[h_] := Exp[-(h/48)] /; h < 24
 m[h_] := Exp[-(h/48)] + Exp[-((h - 24)/48)] /; ( h >= 24 && h < 48)
 m[h_] := Exp[-(h/48)] + Exp[-((h - 24)/48)] + 
     Exp[-((h - 48)/48)] /; ( h >= 48 && h < 72)
 m[h_] := Exp[-(h/48)] + Exp[-((h - 24)/48)] + Exp[-((h - 48)/48)] + 
    Exp[-((h - 72)/48)] /; ( h >= 72 && h < 96)
 m[h_] := Exp[-(h/48)] + Exp[-((h - 24)/48)] + Exp[-((h - 48)/48)] + 
    Exp[-((h - 72)/48)] + Exp[-((h - 96)/48)] /; ( h >= 96 && h < 120)
 Plot[m[h], {h, 0, 120}, AxesOrigin -> {0, 0}]

This is exactly what I want. But now I want to optimize the code, so that I can also Plot[] for periods up to several weeks.

Question: How can I rewrite m[h] such that there will only be one line involved, i.e. add the Exp[-((h - 72)/48] type of terms automatically?

$\endgroup$
4
  • $\begingroup$ You've seen Piecewise[]? $\endgroup$ – J. M.'s ennui Oct 31 '12 at 10:20
  • $\begingroup$ Yeah, but I am looking for a soln without the 'pieces'. I am hoping to get it something like m[h_] = Sum[m, {}]. I don't see it. My mma is getting rusty already. It's only 2 mo since I haven't used it. $\endgroup$ – nilo de roock Oct 31 '12 at 10:32
  • $\begingroup$ For a half life of 24 hours you should be using Exp[-Log[2] h/24] $\endgroup$ – Simon Woods Oct 31 '12 at 13:28
  • $\begingroup$ @Simon, so, 2^(-h/24)? :) $\endgroup$ – J. M.'s ennui Oct 31 '12 at 14:17
6
$\begingroup$

I suppose this function will suit your needs:

m[h_] := Sum[Exp[-((h - 24 k)/48)], {k, 0, Quotient[h, 24]}]

Plot[m[h], {h, 0, 300}, Frame -> True]

plot of m[h]

$\endgroup$
1
  • $\begingroup$ Oh yes, very compact indeed! $\endgroup$ – nilo de roock Oct 31 '12 at 14:02
4
$\begingroup$

Here is a different approach not using Piecewise that calculates the concentration and has the ability to account for variable doses at variable times both in the past and the future for drugs with any given half life.

It uses a list of {dose, time} pairs, time in hours. Negative times are doses received in the past.

concentration[t_, doses_List, halfLifeHours_] := 
 First@# If[t - Last@# <= 0, 0, 
      E^(-((Log[2] (t - Last@#))/halfLifeHours))] & /@ doses // Total

For a drug with unit dose, half life of 24 hours and a past and future doses regime ( 3 days in the past, 3 days into the future ) and a missed current dose.

doses = {{1, -72}, {1, -48}, {1, -24}, {1, 24}, {1, 48}, {1, 72}};

We can plot the resulting concentration:

Plot[concentration[t, doses, 24], {t, -144, 144}, PlotRange -> {{-144, 144}, {0, 3}}, 
     GridLines->Automatic]

Concentration 1

You can even see what would happen with irregularly spaced and incorrectly sized doses:

doses = {{1, -75}, {1, -43}, {.5, -25}, {1, 24}, {1, 48}, {1, 72}};

and thus:

Plot[concentration[t, doses, 24], {t, -144, 144}, 
 PlotRange -> {{-144, 144}, {0, 3}}, GridLines -> Automatic]

Concentration 2

I imagine that a more accurate simulation would include the concentration increase from the point the drug was administered.

$\endgroup$
2
  • $\begingroup$ Very, very good, functionality rich answer but J.M.'s answer provided the most compact representation of the formula and that's what I originally asked for. $\endgroup$ – nilo de roock Nov 2 '12 at 8:41
  • $\begingroup$ @ndroock1 no problem, glad you got some good answers. $\endgroup$ – image_doctor Nov 2 '12 at 10:33
1
$\begingroup$

You can use Piecewise and a little coding (where Accumulate will do the partial sums) :

m2[h_, nDays_] := Piecewise[Transpose[{Accumulate[
 Exp[-(h - #)/48] & /@ (24 Range[0, nDays - 1])],
     #[[1]] <= h < #[[2]] & /@ Partition[24 Range[0, nDays], 2, 1]}]]

m2[h,5]

enter image description here

Plot[m2[h, 5], {h, 0, 120}]

enter image description here

$\endgroup$
7
  • $\begingroup$ Very nice. This already helps. But why the days apart? $\endgroup$ – nilo de roock Oct 31 '12 at 10:42
  • $\begingroup$ Do you mean why the curve is not continuous ? $\endgroup$ – b.gates.you.know.what Oct 31 '12 at 10:46
  • $\begingroup$ No the discontinuity is to be expected. I mean I would like a function from h which is Plot'able and can be used in other Mma calcs. Again: this already helps. Thanks. $\endgroup$ – nilo de roock Oct 31 '12 at 10:49
  • 1
    $\begingroup$ In which way you cannot use it in other calculations ? $\endgroup$ – b.gates.you.know.what Oct 31 '12 at 10:51
  • $\begingroup$ Well, it is not a function of h but from h and nDays. The concentration is only dependent on h. The daily intake is fact and fixed. Do you see the dilemma? $\endgroup$ – nilo de roock Oct 31 '12 at 11:02
1
$\begingroup$

Since Exp is Listable, you can also define your function as

 mm[h_] := Total[Exp[-((h - 24 Range[0, Floor[h/24]])/48)]];
 Plot[mm[h], {h, 0, 300}, Frame -> True]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.