# How to create a composite function from a variable number of components?

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?

-
You've seen Piecewise[]? – J. M. Oct 31 '12 at 10:20
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. – nilo de roock Oct 31 '12 at 10:32
For a half life of 24 hours you should be using Exp[-Log[2] h/24] – Simon Woods Oct 31 '12 at 13:28
@Simon, so, 2^(-h/24)? :) – J. M. Oct 31 '12 at 14:17

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]


-
Oh yes, very compact indeed! – nilo de roock Oct 31 '12 at 14:02

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]


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]


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

-
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. – nilo de roock Nov 2 '12 at 8:41
@ndroock1 no problem, glad you got some good answers. – image_doctor Nov 2 '12 at 10:33

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]


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


-
Very nice. This already helps. But why the days apart? – nilo de roock Oct 31 '12 at 10:42
Do you mean why the curve is not continuous ? – b.gatessucks Oct 31 '12 at 10:46
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. – nilo de roock Oct 31 '12 at 10:49
In which way you cannot use it in other calculations ? – b.gatessucks Oct 31 '12 at 10:51
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? – nilo de roock Oct 31 '12 at 11:02

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]


-