From a Mechanistic Balance to a Function
An ordinary differential equation model specifies how its states change at an instant. To simulate the model, its equations must be expressed as a function that takes the current time, current state, and parameter values, then returns the state derivatives. The function does not itself move the system forward in time: it supplies the rate information that a numerical integration method uses to do so.
This translation is a useful test of whether the model has been written unambiguously. Every production, conversion, or loss term in the biological description must appear with the correct sign and units in the derivative. A function can be syntactically valid and still encode the wrong balance.
A One-Compartment Turnover Model
Consider a hypothetical drug amount \(A\) in a single, well-mixed compartment and a response state \(R\) whose production is inhibited by the drug concentration. The compartment has fixed volume \(V\). Drug is eliminated by first-order clearance \(CL\); during the interval considered, there is no drug input. Drug effect is immediate and depends on the current concentration \(C=A/V\). The response is produced at baseline rate \(k_{\mathrm{in}}\), with a fraction of production inhibited by the drug, and is lost at first order with rate constant \(k_{\mathrm{out}}\).
Assume \(A\) and \(C\) are nonnegative, \(V\), \(CL\), \(IC_{50}\), \(k_{\mathrm{in}}\), and \(k_{\mathrm{out}}\) are positive, and \(0\leq I_{\max}\leq1\). The response \(R\) is also taken to be nonnegative. The fractional inhibition is an Emax-type relationship:
Here \(IC_{50}\) has concentration units, and \(I(C)\) is dimensionless. With these assumptions, \(I(C)\) lies between zero and \(I_{\max}\); therefore the remaining production fraction, \(1-I(C)\), is nonnegative.
For a unit check, if \(A\) is measured in mg, \(V\) in L, time in h, and \(CL\) in L/h, then \(C\) is in mg/L and \(CL\,C\) is in mg/h. If \(R\) is measured in response units, \(k_{\mathrm{in}}\) has units of response units/h and \(k_{\mathrm{out}}\) has units of \(1/\mathrm{h}\), so both terms in \(dR/dt\) are response units/h.
Deriving the RHS and Its Implementation
The first equation follows directly from the amount balance: elimination removes drug at rate \(CL\,C\). Since \(C=A/V\) and \(V\) is fixed, this rate can also be written \(CL\,A/V\). The minus sign indicates loss from the compartment. The response equation adds its two balance terms: the drug-modified production rate is \(k_{\mathrm{in}}(1-I(C))\), and first-order loss removes response at rate \(k_{\mathrm{out}}R\). Substituting the inhibition function gives the form used by the function:
This substitution is also a short check on how the model should be coded: compute concentration from amount, use that concentration in the inhibition term, and subtract the response loss. The derivative function should use the current values of \(A\) and \(R\) each time it is called. It should return both derivatives together, keeping their meaning and units explicit.
In Python-like notation, a named-state implementation can be written as follows. The function receives time \(t\), a state record with fields “A” and “R,” and a parameter record with fields “V,” “CL,” “kin,” “kout,” “Imax,” and “IC50.” It calculates \(C=\text{state["A"]}/\text{p["V"]}\), then calculates \(\text{inhibition}=\text{p["Imax"]}*C/(\text{p["IC50"]}+C)\). It sets \(\text{dA}=-\text{p["CL"]}*C\) and \(\text{dR}=\text{p["kin"]}*(1-\text{inhibition})-\text{p["kout"]}*\text{state["R"]}\), and returns the named record \(\{"A":\text{dA},"R":\text{dR}\}\).
Time \(t\) is part of the general function interface because models may depend explicitly on time. In this particular model, the right-hand side has no explicit time dependence: if the state and parameters are unchanged, its returned derivatives are unchanged. The function still accepts \(t\), but does not need to use it. The returned fields represent rates, not updated values; for example, returning \(A+\text{dA}\) would not be an RHS implementation.
Worked Example: Evaluating the Function at a Given State
Use hypothetical values \(A=120\) mg, \(R=80\) response units, \(V=20\) L, \(CL=2\) L/h, \(k_{\mathrm{in}}=8\) response units/h, \(k_{\mathrm{out}}=0.10\) h\(^{-1}\), \(I_{\max}=0.75\), and \(IC_{50}=3\) mg/L. Calculate the RHS output.
First calculate concentration: \(C=A/V=120\text{ mg}/20\text{ L}=6\) mg/L. Then the fractional inhibition is \(I(C)=0.75(6)/(3+6)=4.5/9=0.50\). Thus 50% of baseline production remains.
The amount derivative is \(dA/dt=-CL\,C=-(2\text{ L/h})(6\text{ mg/L})=-12\) mg/h. The response production rate is \((8\text{ response units/h})(1-0.50)=4\) response units/h, and its loss rate is \((0.10\text{ h}^{-1})(80\text{ response units})=8\) response units/h. Therefore \(dR/dt=4-8=-4\) response units/h.
The function returns the rates \(-12\) mg/h for \(A\) and \(-4\) response units/h for \(R\). Both are negative at this state: drug amount is being eliminated, and response loss currently exceeds response production.
Checks That Connect the Code to the Biology
Before using an RHS function in a larger model, check that each expression corresponds to the stated balance. A practical review follows the terms from model to implementation rather than relying on whether the code runs:
Confirm that concentration is calculated as current amount divided by the fixed volume, and that its units are concentration.
Enter elimination and response loss with negative signs; enter production with a positive sign, reduced by the inhibited fraction.
Each returned rate must have the state’s units divided by time. Terms added within a derivative must have matching units.
At a chosen state and parameter set, reproduce every intermediate value and returned derivative independently.
The model also supplies simple limiting checks. When \(A=0\), concentration and inhibition are zero, so the drug derivative is zero and response production is at baseline. When \(C\) is large relative to \(IC_{50}\), inhibition approaches \(I_{\max}\), rather than exceeding it. At any fixed concentration, the response derivative is zero when production balances loss:
In the absence of inhibition, this balance reduces to \(R=k_{\mathrm{in}}/k_{\mathrm{out}}\). These checks do not prove that every implementation detail is correct, but they can reveal a misplaced sign, a missing denominator, or use of the wrong state in a term.
Worked Example: Checking the No-Drug Boundary
Set \(A=0\) mg and \(R=50\) response units. Use hypothetical parameters \(V=10\) L, \(CL=1.5\) L/h, \(k_{\mathrm{in}}=5\) response units/h, \(k_{\mathrm{out}}=0.10\) h\(^{-1}\), \(I_{\max}=0.8\), and \(IC_{50}=2\) mg/L. Evaluate the derivatives and explain the result.
The concentration is \(C=0/10=0\) mg/L, so \(I(C)=0.8(0)/(2+0)=0\). The amount derivative is \(dA/dt=-(1.5)(0)=0\) mg/h. The response derivative is \(dR/dt=5(1-0)-(0.10)(50)=5-5=0\) response units/h.
There is no drug to eliminate, and the response is at its uninhibited balance \(k_{\mathrm{in}}/k_{\mathrm{out}}=5/0.10=50\) response units. A nonzero drug derivative or a nonzero response derivative at this state would signal either a mismatch between the intended model and the code or an arithmetic error.
Worked Example: Detecting an Incorrect Loss Sign
Use the first example’s state and parameters, but suppose the implementation mistakenly adds response loss: \(dR/dt=k_{\mathrm{in}}(1-I(C))+k_{\mathrm{out}}R\). Work out the returned response derivative and compare it with the intended balance.
At \(C=6\) mg/L, \(I(C)=0.50\), so production is \(8(1-0.50)=4\) response units/h. The mistaken positive loss term is \(0.10(80)=8\) response units/h. The incorrect derivative is therefore \(4+8=12\) response units/h, while the intended derivative is \(4-8=-4\) response units/h.
The wrong sign predicts that response rises even though production is below loss. Checking the sign of each balance term against its biological role, then verifying the arithmetic at a concrete state, identifies the error. Merely checking that the function returns a number would not.
What the RHS Does—and Does Not Do
An RHS function provides an instantaneous rate. For example, \(dA/dt=-12\) mg/h at a particular state does not mean that the amount after one hour is exactly \(108\) mg. The rate can change as the state changes. A numerical integration method repeatedly evaluates the RHS and combines those evaluations to approximate the state trajectory. That integration procedure is distinct from the derivative function itself.
This distinction also clarifies the main alternative to an RHS implementation. One could try to update the state directly with a finite time step, such as \(A_{\mathrm{new}}=A+\Delta t(dA/dt)\). That is an integration step, not a replacement definition of the model’s derivative; its accuracy depends on the step size and the changing rate. Keeping the mechanistic RHS separate makes it possible to inspect and test the balance independently of a particular time-stepping method.
The function shown here is deliberately limited to a fixed-volume compartment, first-order elimination, immediate concentration-dependent inhibition, and first-order response loss. It does not represent changing volume, delayed drug effect, saturable elimination, or drug input. Such mechanisms require different balance terms and, when appropriate, additional states or explicit time dependence. Do not add them implicitly through a code shortcut: state the biological assumption and derive the corresponding term first.
Common Mistakes and Failure Modes
- Returning an updated state instead of a derivative. The RHS returns \(dA/dt\) and \(dR/dt\), with units per time. A state plus a rate is not the requested output.
- Using amount where concentration is required. The inhibition law uses \(C=A/V\). Substituting \(A\) directly makes the expression dimensionally inconsistent and changes the modeled effect.
- Omitting the loss sign. First-order response loss subtracts \(k_{\mathrm{out}}R\). A positive sign reverses the biological effect.
- Mixing units or scales. \(IC_{50}\) and \(C\) must use the same concentration units, and the clearance and volume units must make \(CL/V\) an inverse time.
- Assuming successful execution verifies the model. A function may run while using a wrong rate constant or state. Hand calculations, units, signs, and boundary checks test meaning as well as syntax.
A RHS function is most useful when it is a direct, readable expression of the model’s balances. For this turnover example, every returned derivative can be traced to a named biological term, and each can be checked at a specific state. That traceability is more valuable than compressing the code into a single opaque expression.
Check Your Understanding
Answer each question, then compare with the concise solutions below.
- If \(A\) is in mg, \(V\) in L, and \(CL\) in L/h, what are the units of \(C\), \(CL/V\), and \(dA/dt\)?
- At \(C=IC_{50}\), what fraction of baseline production remains when \(I_{\max}=0.6\)?
- For \(k_{\mathrm{in}}=6\) response units/h, \(k_{\mathrm{out}}=0.2\) h\(^{-1}\), and \(I(C)=0.25\), what response value makes \(dR/dt=0\)?
- Why is returning \(A+dA/dt\) an incorrect RHS result?
- At \(A=0\), what are the drug derivative and inhibition under this model, and which assumptions make that result hold?
Solutions. (1) \(C\) is mg/L, \(CL/V\) is h\(^{-1}\), and \(dA/dt=-CL\,C\) is mg/h. (2) At \(C=IC_{50}\), the inhibited fraction is \(I_{\max}/2=0.30\), so the remaining production fraction is \(1-0.30=0.70\). (3) Set \(0=6(1-0.25)-0.2R\), giving \(R=4.5/0.2=22.5\) response units. (4) An RHS must return a rate with units of state per time; \(A+dA/dt\) adds quantities with incompatible units and is an update-like expression, not the derivative. (5) Since \(C=A/V=0\), inhibition is zero and \(dA/dt=-CL\,C=0\). This uses the fixed positive volume, the specified concentration relationship, and the absence of an input term in the amount balance.