| 1 |
\name{timelags} |
| 2 |
\alias{timelags} |
| 3 |
\alias{lagvalue} |
| 4 |
\alias{lagderiv} |
| 5 |
\title{ |
| 6 |
Time Lagged Values of State Variables and Derivatives. |
| 7 |
} |
| 8 |
\description{ |
| 9 |
Functions \code{lagvalue} and \code{lagderiv} provide access to past |
| 10 |
(lagged) values of state variables and derivatives. |
| 11 |
|
| 12 |
They are to be used with function \code{dede}, to solve delay differential |
| 13 |
equations. |
| 14 |
} |
| 15 |
\usage{ |
| 16 |
lagvalue(t, nr) |
| 17 |
lagderiv(t, nr) |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{t }{the time for which the lagged value is wanted; this should |
| 21 |
be no larger than the current simulation time and no smaller than the |
| 22 |
initial simulation time. |
| 23 |
} |
| 24 |
\item{nr }{the number of the lagged value; if \code{NULL} then all state |
| 25 |
variables or derivatives are returned. |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
\value{ |
| 30 |
a scalar (or vector) with the lagged value(s). |
| 31 |
} |
| 32 |
\author{Karline Soetaert <[email protected]>} |
| 33 |
|
| 34 |
\details{ |
| 35 |
The \code{lagvalue} and \code{lagderiv} can only be called during the |
| 36 |
integration, the lagged time should not be smaller than the initial |
| 37 |
simulation time, nor should it be larger than the current simulation |
| 38 |
time. |
| 39 |
|
| 40 |
Cubic Hermite interpolation is used to obtain an accurate interpolant |
| 41 |
at the requested lagged time. |
| 42 |
} |
| 43 |
\seealso{ |
| 44 |
\link{dede}, for how to implement delay differential equations. |
| 45 |
} |
| 46 |
\examples{ |
| 47 |
## ============================================================================= |
| 48 |
## exercise 6 from Shampine and Thompson, 2000 |
| 49 |
## solving delay differential equations with dde23 |
| 50 |
## |
| 51 |
## two lag values |
| 52 |
## ============================================================================= |
| 53 |
|
| 54 |
##----------------------------- |
| 55 |
## the derivative function |
| 56 |
##----------------------------- |
| 57 |
derivs <- function(t, y, parms) { |
| 58 |
History <- function(t) c(cos(t), sin(t)) |
| 59 |
if (t < 1) |
| 60 |
lag1 <- History(t - 1)[1] |
| 61 |
else |
| 62 |
lag1 <- lagvalue(t - 1)[1] # returns a vector; select first element |
| 63 |
|
| 64 |
if (t < 2) |
| 65 |
lag2 <- History(t - 2)[2] |
| 66 |
else |
| 67 |
lag2 <- lagvalue(t - 2,2) # faster than lagvalue(t - 2)[2] |
| 68 |
|
| 69 |
dy1 <- lag1 * lag2 |
| 70 |
dy2 <- -y[1] * lag2 |
| 71 |
|
| 72 |
list(c(dy1, dy2), lag1 = lag1, lag2 = lag2) |
| 73 |
} |
| 74 |
|
| 75 |
##----------------------------- |
| 76 |
## parameters |
| 77 |
##----------------------------- |
| 78 |
|
| 79 |
r <- 3.5; m <- 19 |
| 80 |
|
| 81 |
##----------------------------- |
| 82 |
## initial values and times |
| 83 |
##----------------------------- |
| 84 |
|
| 85 |
yinit <- c(y1 = 0, y2 = 0) |
| 86 |
times <- seq(0, 20, by = 0.01) |
| 87 |
|
| 88 |
##----------------------------- |
| 89 |
## solve the model |
| 90 |
##----------------------------- |
| 91 |
|
| 92 |
yout <- dede(y = yinit, times = times, func = derivs, |
| 93 |
parms = NULL, atol = 1e-9) |
| 94 |
|
| 95 |
##----------------------------- |
| 96 |
## plot results |
| 97 |
##----------------------------- |
| 98 |
|
| 99 |
plot(yout, type = "l", lwd = 2) |
| 100 |
|
| 101 |
## ============================================================================= |
| 102 |
## The predator-prey model with time lags, from Hale |
| 103 |
## problem 1 from Shampine and Thompson, 2000 |
| 104 |
## solving delay differential equations with dde23 |
| 105 |
## |
| 106 |
## a vector with lag valuess |
| 107 |
## ============================================================================= |
| 108 |
|
| 109 |
##----------------------------- |
| 110 |
## the derivative function |
| 111 |
##----------------------------- |
| 112 |
predprey <- function(t, y, parms) { |
| 113 |
tlag <- t - 1 |
| 114 |
if (tlag < 0) |
| 115 |
ylag <- c(80, 30) |
| 116 |
else |
| 117 |
ylag <- lagvalue(tlag) # returns a vector |
| 118 |
|
| 119 |
dy1 <- a * y[1] * (1 - y[1]/m) + b * y[1] * y[2] |
| 120 |
dy2 <- c * y[2] + d * ylag[1] * ylag[2] |
| 121 |
list(c(dy1, dy2)) |
| 122 |
} |
| 123 |
|
| 124 |
##----------------------------- |
| 125 |
## parameters |
| 126 |
##----------------------------- |
| 127 |
|
| 128 |
a <- 0.25; b <- -0.01; c <- -1 ; d <- 0.01; m <- 200 |
| 129 |
|
| 130 |
##----------------------------- |
| 131 |
## initial values and times |
| 132 |
##----------------------------- |
| 133 |
|
| 134 |
yinit <- c(y1 = 80, y2 = 30) |
| 135 |
times <- seq(0, 100, by = 0.01) |
| 136 |
|
| 137 |
#----------------------------- |
| 138 |
# solve the model |
| 139 |
#----------------------------- |
| 140 |
|
| 141 |
yout <- dede(y = yinit, times = times, func = predprey, parms = NULL) |
| 142 |
|
| 143 |
##----------------------------- |
| 144 |
## display, plot results |
| 145 |
##----------------------------- |
| 146 |
|
| 147 |
plot(yout, type = "l", lwd = 2, main = "Predator-prey model", mfrow = c(2, 2)) |
| 148 |
plot(yout[,2], yout[,3], xlab = "y1", ylab = "y2", type = "l", lwd = 2) |
| 149 |
|
| 150 |
diagnostics(yout) |
| 151 |
|
| 152 |
## ============================================================================= |
| 153 |
## |
| 154 |
## A neutral delay differential equation (lagged derivative) |
| 155 |
## y't = -y'(t-1), y(t) t < 0 = 1/t |
| 156 |
## |
| 157 |
## ============================================================================= |
| 158 |
|
| 159 |
##----------------------------- |
| 160 |
## the derivative function |
| 161 |
##----------------------------- |
| 162 |
derivs <- function(t, y, parms) { |
| 163 |
tlag <- t - 1 |
| 164 |
if (tlag < 0) |
| 165 |
dylag <- -1 |
| 166 |
else |
| 167 |
dylag <- lagderiv(tlag) |
| 168 |
|
| 169 |
list(c(dy = -dylag), dylag = dylag) |
| 170 |
} |
| 171 |
|
| 172 |
##----------------------------- |
| 173 |
## initial values and times |
| 174 |
##----------------------------- |
| 175 |
|
| 176 |
yinit <- 0 |
| 177 |
times <- seq(0, 4, 0.001) |
| 178 |
|
| 179 |
##----------------------------- |
| 180 |
## solve the model |
| 181 |
##----------------------------- |
| 182 |
|
| 183 |
yout <- dede(y = yinit, times = times, func = derivs, parms = NULL) |
| 184 |
|
| 185 |
##----------------------------- |
| 186 |
## display, plot results |
| 187 |
##----------------------------- |
| 188 |
|
| 189 |
plot(yout, type = "l", lwd = 2) |
| 190 |
} |
| 191 |
|
| 192 |
\keyword{utilities} |