Arne - 24 days ago 5x
R Question

# R: handing over parameters to function (deSolve-package)

I recently downloaded the deSolve-package to solve ODE-models. I programmed a model according to some example code I found, yet, there seems to be a problem with the handing over of parameters since I get an error massage about unknown parameters.

``````  library(deSolve)

model <- function(t, y, parms) {
dY1 = -(k1 * y[1]) + (k2 * y[6]) - (k13 * y[1] * 400*sin(((2*pi)/period_ca)*t-phase_ca)+600) + (k14 * y[2]) - (k17 * y[1] * 400*sin(((2*pi)/period_dg)*t-phase_dg)+600) + (k18 *y[11]) - (k3*y[1] * AA) + (k4 * y[7])
dY2 = (k13 * y[1] * 400*sin(((2*pi)/period_ca)*t-phase_ca)+600) - (k14 * y[2]) - (k15 * y[2] *400*sin(((2*pi)/period_dg)*t-phase_dg)+600) + (k16 * y[3]) - (k5 * y[2]) + (k6 * y[8]) - (k7 * y[2] * AA) + (k8 * y[9])
dY3 = (k15 * y[2] * 400*sin(((2*pi)/period_dg)*t-phase_dg)+600) - (k16 * y[3]) - (k9 * y[3]) + (k10 * y[10])
dY4 = -(k11 * y[4]) + (k12 * y[5]) + (k19 * y[11] * AA) - (k20 * y[4])
dY5 =  (k11 * y[4]) - (k12 * y[5])
dY6 = (k1 * y[1]) - (k2 * y[6])
dY7 = (k3 + y[1] * AA) - (k4 * y[7])
dY8 = (k5 * y[2]) - (k6 * y[8])
dY9 = (k7 * y[2] * AA) - (k8 * y[9])
dY10 = (k9 * y[3]) - (k10 * y[10])
dY11 = (k17 * y[1] * 400*sin(((2*pi)/period_dg)*t-phase_dg)+600) - (k18 * y[11]) - (k19 * y[11] * AA) + (k20 * y[4])
list(c(dY1, dY2, dY3,dY4, dY5, dY6,dY7, dY8, dY9, dY10, dY11))
}

yini <- c(y1 = 1000, y2 = 0, y3 = 0, y4 = 0, y5 = 0, y6 = 20, y7 = 0, y8 =    0, y9 = 0, y10 = 0, y11 = 0)
times <- seq(from = 0, to = 5000, by = 0.1)
parms <- c(AA=11000, k1=1, k2=50, k3=1.2e-7, k4=0.1, k5=1.2705, k6=3.5026, k7=1.2e-6, k8=0.1, k9=1, k10=0.1, k11=2, k12=0.2, k13=0.0006, k14=0.5, k15=7.998e-6,
k16=8.6348, k17=6e-7, k18=0.1, k19=1.8e-5, k20=2, period_ca=100, phase_ca=0, period_dg=100,
phase_dg=0)
out <- ode (times = times, y = yini, func = model, parms = parms)
``````

Here dY1 to dY11 represent differential equations for certain system components. Parms is a vector defining the necessary parameter values, yini defines the initial conditions and times the time scale.

I receive the following error message:

I am quite new to R and do not understand the origin of the problem (all example code I found was constructed the same way).

You have to look up some examples from the `deSolve` package. To use your parameters in the function, you need to use the `with` function:

``````model <- function(t, y, parms) {
with(as.list(c(y, parms)), {
dY1 = ...
.
.
.
dY11 = ...

list(c(dY1, dY2, dY3,dY4, dY5, dY6,dY7, dY8, dY9, dY10, dY11))
})
}
``````

Nevertheless I got an error, because the integration fails, but this occurs not because the code is wrong. Perhaps this is not the case on your PC.

You can try this code for solving the ODE, which gives some warnings, but integration is successful (on my PC):

``````out <- ode(times = times, y = yini, func = model, parms = parms, method = "bdf")
``````