An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-dynamic-models/attachments/20130827/df027321/attachment.pl>
[R-sig-dyn-mod] Multiple Systems of ODE with Variable Coefficents
2 messages · Munjal Patel, Thomas Petzoldt
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