rootSolve: return(jacobian.full vs multiroot) how to alternate

In the following script I try to use a single function that will be
used in multiroot and jacobian.full.
But it is impossible for me, the function for one case must revert a vector and for another a list. As I have commented on the body of the function.
What would be a possible solution. Or should I use another package; for example numDeriv :: jacobian?

library(rootSolve)
sys <- function( y, parms) {
  with(as.list(c(y,parms)),{
    
    dy = a*(1 + b*(y[2] - 1)/(c + y[2] - 1) - 1*y[1])
    dz = 1*y[1] - y[2]
    #to multiroot, it must return a vector with as many values as the length of start
    return(c(dy, dz))
    #to jacobian,function that calculates one function value for each element of y
    return(list(c(dy, dz))
  })
}

parms <- list(a = 1, b = 1, c= 2)
ic=multiroot(f=sys, start=c(-1, 18), parms = parms)
ic$root

jacobian.full(y = ic$root, func =sys, parms = parms)

library(deSolve)
ode <- deSolve::ode(y = c( 1,  0), 
                    times = seq(1,100), 
                    func = sys, 
                    parms = parms)


plot(ode)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.