title: using a time series of parameters to solve ODE in R
body: <p>I am trying to solve a simple ODE in R using deSolve: <code>dQ/dt = f(Q)*(P - E).</code>The whole thing is a time series of Q. The trick is that P and E are fixed time series of data themselves, so the diff eq is effectively in Q alone. </p>
  
  <p>It's relatively straightforward to solve this iteratively with a fixed time step, but I am trying to find a way to use an adaptive time step in R. Because P and E are discrete, successive timesteps may have the same value of P and E, which is fine. I have been playing around with deSolve, but have not been able to work this out. Ideally I would like to use standard 4th order Runge-Kutta. </p>
  
  <p>Any ideas? Do it in MATLAB?</p>
  
  <p>Edited for reproduceable example. I would like to be able to do this calc with a variable time step Runge-Kutta 4 method. I could program a fixed time step rk4 pretty easily, not so much adaptive.</p>
  
  <pre><code>working &lt;- structure(list(datetime = structure(c(1185915600, 1185919200, 
  1185922800, 1185926400, 1185930000, 1185933600, 1185937200, 1185940800, 
  1185944400, 1185948000, 1185951600), class = c("POSIXct", "POSIXt"
  ), tzone = "UTC"), p = c(0, 0, 0, 1.1, 0.5, 0.7, 0, 0, 1.3, 0, 
  0), e = c(0.15, 0.14, 0.13, 0.21, 0.15, 0.1, 0.049, 0, 0, 0, 
  0), qsim = c(-1.44604436552566, NA, NA, NA, NA, NA, NA, NA, NA, 
  NA, NA)), .Names = c("datetime", "p", "e", "qsim"), row.names = c(NA, 11L), 
  class = "data.frame")
  
  
  # this is the derivative function dQ/dt = f(Q,p,e) where p and e are time series
  dqdt &lt;- function(qsim, pcp, pet) {
    dq &lt;- (qsim ^ 2) * ((pcp - pet) / qsim) 
    return(dq)
  }
  
  # loops through and calculates the Q at each time step using the Euler method
  for (i in 1:(nrow(working) - 1)) {
    dq &lt;- dqdt(working$qsim[i], pcp = working$p[i], pet = working$e[i])
    working$qsim[i + 1] &lt;- working$qsim[i] + dq
  }
  </code></pre>