rode

Solving a single ODE in R


I'm new to R and I'm trying to construct a minimal working example for solving an ODE. I want to solve dy/dt = y with initial condition y(0) = 1. I don't have any parameters, so I tried putting params = NULL and I also tried omitting the argument altogether, which gave me the following error:

Error in func(time, state, parms, ...) : unused argument (parms).

Given that I don't have any parameters, I'm not sure what to do. My code is below.

library(deSolve)
dydt <- function(y,t) {
     ydot <- y
     return(ydot)
}
tvals = c(0:5)
y0 = 1

out <- ode(y = y0, times = tvals, func = dydt, parms = NULL) 

Solution

  • library(deSolve)
    dydt <- function(t,y,parms) {
      ydot <-y
      list(ydot)
    }
    tvals = c(0:5)
    y0 =1
    out <- ode(y = y0, parms=NULL,times = tvals, func = dydt)
    

    As you can see from ?ode:

    1. If func is an R-function, it must be defined as: func <- function(t, y, parms,...) So dydt need a third parameter
    2. the return value of func should be a list So you need list(ydot) instead of return(ydot)

    Best!