[R-sig-dyn-mod] Multiple Systems of ODE with Variable Coefficents

Thomas Petzoldt Thomas.Petzoldt at tu-dresden.de
Tue Aug 27 16:51:36 CEST 2013


Hi,

what about the following?

-- ThomasP


library(deSolve)

## an example function
f <- function(t, y) y^-t

## the differential equations
model <- function(t, y, k) {

   ## k[2] is a function of t and y[2]
   k[2] <- f(t, y[2])

   ## the derivatives
   dy1 <- k[1] * y[1] + k[2] * y[2] * y[3]
   dy2 <- k[1] * y[1] + k[2] * y[2] * y[3] - k[3] * y[2] * y[2]
   dy3 <- k[3] * y[2] * y[2]

   ## first argument of the list: derivatives (dy1 ... dy3)
   ## second argument:            auxiliary outputs (k2)
   list(c(dy1, dy2, dy3), k2 = k[2])
}

y0 <- c(y1 = 200, y2 = 0, y3 = 0)
k  <- c(0.1, 0, 0.1)     # k[2] is only a placeholder
times <- seq(0, 10, 0.1)

out <- ode(y0, times, model, k)
plot(out)
out



More information about the R-sig-dynamic-models mailing list