title: Numerically solving a system of ODEs in R with a for loop
body: <p>I am trying to solve a series of equations using deSolve in R. I wish to list the equations using for loop but having trouble doing so.
  The system is as follows:</p>
  
  <p>X_i' = a X_i+1 - (b+c) X_i</p>
  
  <p>X_k' = 2c (X_1+ ... + X_k) - (b+c) X_k</p>
  
  <p>This is how I tried to write it:</p>
  
  <pre><code>library(deSolve)
  ode_func &lt;- function(t, state, parms){
  with(as.list(c(state, parms)),{
  
  #rate of change
  for (i in 1:k-1) {
  dX[i] &lt;-  a * X[i+1] - (b + c) * X[i]
  dX[k] &lt;-  2 * c * sum(X[i]) - (b + c) * X[k]
  
  #return the rate of change
  list(c(dX[i], dX[k])) } }) }
  
  k=10
  #initial conditions
  state &lt;- c(rep(0, k-1), 1e4)
  times=seq(0,500,1)
  
  sol_ode &lt;- ode(y= state, times=times, func = ode_func,
             parms = list(a= 0.01, b= 0.01, alpha = 0.01))
  </code></pre>
  
  <p>I get and error saying that 'object X not found'. I have used deSolve many times successfully but had bever had to use for loop. Thanks in advance for the help.</p>