# WhenEvent versus If function for ODEs

The following equation works for ParametricNDSolve


PKmodel =
{
Amt1'[t] ==
If[0 < Mod[t, \[Tau]] < InfusionTime, Dose/InfusionTime, 0] -
CL/V1 Amt1[t] - CLD/V1 Amt1[t] + CLD/V2 Amt2[t] -
Vmax Amt1[t]/V1/(Km + Amt1[t]/V1),
Amt2'[t] == CLD/V1 Amt1[t] - CLD/V2 Amt2[t],
Amt1[0] == 0,
Amt2[0] == 0
};


However, the following code did not work for ParametricNDSolve

PKmodel =
{
Amt1'[t] == -CL/V1 Amt1[t] - CLD/V1 Amt1[t] + CLD/V2 Amt2[t] -
Vmax Amt1[t]/V1/(Km + Amt1[t]/V1),
Amt2'[t] == CLD/V1 Amt1[t] - CLD/V2 Amt2[t],
Amt1[0] == 0,
Amt2[0] == 0,
WhenEvent[0 < Mod[t, \[Tau]] < InfusionTime,
Amt1'[t] -> Amt1'[t] + Dose/InfusionTime]
}


Thus, WhenEvent could not work for an input that is over a duration of time? Any reason for this?

Second, by using If function, the ParametricNDSolve works for solving the ODEs, but I have some numerical issue when execute the following function, although mathematica still gives me the results of AUC across doses.

AUClist =
NIntegrate[
Evaluate[Table[(Amt1[Dose][t] /. solns)/(47.9*75), {Dose,
doselistug}]], {t, 0, tmax}] (*ug/mL*day*)


Any suggestions for solving this?

• An event (in the case of WhenEvent) occurs when the predicate changes from False to True, according to the docs. Hence events occur at discrete times and do not happen over a duration. – Michael E2 May 14 at 2:58
• So Fasle and True could not happen over a duration in Mathematica? That is good to know. What about numerical issue of NIntegrate for the If function? – Math Entry May 14 at 3:53
• I tested the following code by specifying values for t and tau and InfusionTime and the mathematica return to me True or False: 0 < Mod[t, \[Tau]] < InfusionTime. With the change of t, 0 < Mod[t, \[Tau]] < InfusionTime will take different values of True or False. Why it is not allowed in WhenEvent and ParametricNDSolve? – Math Entry May 14 at 4:13
• As @MichaelE2 said, WhenEvent only watches for when the first argument changes from False to True -- not the reverse. What I was thinking of when I mentioned WhenEvent earlier was treating the infusion as a discrete pulse: WhenEvent[t==Mod[t, \[Tau]], Amt1[t] -> Amt1[t] + Dose]. – Chris K May 14 at 7:19
• In general, try to include all the required info in your questions. For example, how did you call ParametricNDSolve and what numerical issues does that NIntegrate cause? – Chris K May 14 at 7:20