Search code examples
rodedifferential-equations

Error while plotting ODE phase potriat in R package phaseR


I am trying to plot a two-dimensional phase portrait in R using the phaseR package. This is an example of what I want to do:

Example that works

library(phaseR)

lotkaVolterra <- function(t, y, parameters) {
x <- y[1]
y <- y[2]
lambda  <- parameters[1]
epsilon <- parameters[2]
eta     <- parameters[3]
delta   <- parameters[4]
dy    <- numeric(2)
dy[1] <- lambda*x - epsilon*x*y
dy[2] <- eta*x*y - delta*y
list(dy)
}

then when I plot it I get

lotkaVolterra.flowField <- flowField(lotkaVolterra, x.lim = c(0, 5), y.lim = c(0, 10), parameters = c(2, 1, 3, 2), points = 19, add = FALSE)

grid()


lotkaVolterra.nullclines <- nullclines(lotkaVolterra, x.lim = c(-1, 5), y.lim = c(-1, 10), parameters = c(2, 1, 3, 2), points = 500)

y0 <- matrix(c(1, 2, 2, 2, 3, 4), ncol = 2, nrow = 3, byrow = TRUE)

lotkaVolterra.trajectory <- trajectory(lotkaVolterra, y0 = y0, t.end = 10, parameters = c(2, 1, 3, 2), colour = rep("black", 3))

this is the plot I get:

enter image description here

The problem

When I try to do the same with my equation however the vector space does not appear:

WalpeFun <- function(t, y, parameters) {
  x <- y[1]
  y <- y[2]
  k <- parameters[1]
  z <- parameters[2]
  w <- parameters[3]
  b <- parameters[4]
  d <- parameters[5]
  v <- parameters[6]
  a <- parameters[7]
  g <- parameters[8]
  l <- parameters[9]
  e <- parameters[10]
  dy <- numeric(2)
  dy[1] <- 2.5*(1-(x/k)^z)+g*l+w*e - b*(x*y/d^2+y^2)
  dy[2] <- 2.5 * (1 - (y/x + v)^a)
  list(dy)
}


Walpe.flowField <-flowField(WalpeFun, x.lim = c(0, 150), y.lim = c(-1, 50), parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273) , points = 20, add = FALSE)

grid()

Walpe.nullclines <-nullclines(WalpeFun, x.lim = c(0, 150), y.lim = c(-1, 50), parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273))

y0 <- matrix(c(8.2, 2), ncol = 2, nrow = 1, byrow = TRUE)

Walpe.trajectory <-trajectory(WalpeFun, y0 = y0, t.end = 100, parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273),system = "two.dim", colour = "black")

I get this very different plot:

enter image description here

and get the following error:

Error in if ((dx[i, j] != 0) & (dy[i, j] != 0)) { : missing value where TRUE/FALSE needed

I don't understand why the vectors don show, or why the blue nullcline is missing


Solution

  • Mathematically your x.lim range exceeds the domain where the function can have a value. Because your dy[2] expression has x in the denominator of one of its terms, the function blows up at x == 0 and then there will be an NA in the dy[]-matrix that is internal to the function code. (There's a bit of an ambiguity in that your dy-object is a 2 element vector whereas looking at the code, the calculations are being stored in 2d-matrices named dx and dy.)

    flowField  #look at the code
    png()
    Walpe.flowField <-flowField(WalpeFun, x.lim = c(0.01, 150), y.lim = c(-1, 50), parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273) , points = 20, add = FALSE, system="two.dim")
    Walpe.nullclines <-nullclines(WalpeFun, x.lim = c(0.01, 150), y.lim = c(-1, 50), parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273))
    
    y0 <- matrix(c(8.2, 2), ncol = 2, nrow = 1, byrow = TRUE)
    
    Walpe.trajectory <-trajectory(WalpeFun, y0 = y0, t.end = 100, parameters = c(120.73851, 0.51786, -0.75178, 0.00100, 1.00000, 500, 0.001, 0.01102, 320.995455, 5.582273),system = "two.dim", colour = "black")
    dev.off()
    

    enter image description here

    I don't know why the nullclines don't appear, but I'm guessing there are features of the function that neither of us understands.