# Numerical solving Lotka-Volterra ODE in R

Aim: I am trying to numerically solve a Lotka-Volterra ODE in R, using de sde.sim() function in the sde package. I would like to use the sde.sim() function in order to eventually transform this system into an SDE. So initially, I started with a simple ODE system (Lotka-Volterra model) without a noise term.

The Lotka-Volterra ODE system: $$\left\{\begin{matrix}\frac{dx}{dt}= \alpha x -\beta xy. \\ \frac{dy}{dt}= \delta xy -\gamma y \end{matrix}\right.$$

with initial values for x = 10 and y = 10.

The parameter values for alpha, beta, delta and gamma are 1.1, 0.4, 0.1 and 0.4 respectively (mimicking this example).

Attempt to solve the problem:

library(sde)
d <- expression((1.1 * x - 0.4 * x * x), (0.1 * x * x - 0.4 * x))
s <- expression(0, 0)
X <- sde.sim(X0=c(10,10), T = 10, drift=d, sigma=s)
plot(X)


However, this does not seem to generate a nice cyclic behavior of the predator and prey population.

Question:

What is going wrong in the attempt to solve this system of ODEs in sde.sim()?

• I'm not so sure about the R sde package, but I know diffeqr can handle it because DifferentialEquations.jl can. You'd just need to translate this example over to this syntax in order to use it from R. Feb 12, 2019 at 13:39

I did not yet succeed to use the sde.sim() function of the SDE package, however, I succeeded to solve the system (with and without noise) using the suggestions of Chris and the diffeqr package in R.

library(plotly)

# Lotka-Volterra Model (SDE without noise)
f <- function(u,p,t) {
du1 = p*u-p*u*u
du2 = p*u*u-p*u
return(c(du1,du2))
}

g <- function(u,p,t) {
return(c(0*u,0*u))
}

u0 = c(10.0, 10.0)
tspan <- list(0.0,100.0)
p = c(1.1, 0.4, 0.1, 0.4)
sol = diffeqr::sde.solve(f,g,u0,tspan,p=p,saveat=0.005)
udf = as.data.frame(sol$u) plot_ly(udf, x = sol$t, y = ~V1, name = 'Prey', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~V2, name = 'Predator', mode = 'lines')


So, not the solution to the problem, but a step in the right direction.