简体   繁体   中英

R - deSolve,parameters

i'm new to this site. I'm implementing a script in R, and i need to utilize deSolve, in order to do that i have to define the parameters that are useful when i write the "rate of change" of my equations.

Here's my problem, for using the package i'm obliged to define those parameters, but I actually don't need them, I mean in the rate of change of my equations i don't have any parameters so i'm asking for a way to avoid their definition.

I partially solved that problem defining the parameters in this way(not very elegant):

parameters <- c(1)

but I don't like this kind of solution.

Thank you in advance for any help, if my message is not that clear please report it to me, sorry but english is not my first language!

That's the code:

parameters <- c(1)
e=0.2056

ini <- c(q1 =1-e, q2 = 0, p1 = 0, p2 = sqrt ((1+e)/(1-e)) )

rhs <- function(t, ini,parameters)
{
   with(as.list(c(ini,parameters)),{
    # rate of change
    dq1 <- p1
    dq2 <- p2
    dp1 <- -q1/((q1^2+q2^2)^(3/2))
    dp2 <- -q2/((q1^2+q2^2)^(3/2))
    # return rate of change
    list(c(dq1,dq2,dp1,dp2))
  } )
}

###### EULER ######

library(deSolve)
times <- seq(0,40, by = 0.0005)

out<- ode(y = ini, times = times, func = rhs, parms = parameters,method="euler")
head(out)

'ode' needs those parameters, it says error if i don't put parms

parms is passed to the method that actually does the solving, and in turn passed to your supplied function, and thus must be supplied. But it doesn't need to have a value, and your function doesn't need to use it.

# Accept and ignore third argument
rhs <- function(t, ini,...)
{
    with(as.list(c(ini)),{
        # rate of change
        dq1 <- p1
        dq2 <- p2
        dp1 <- -q1/((q1^2+q2^2)^(3/2))
        dp2 <- -q2/((q1^2+q2^2)^(3/2))
        # return rate of change
        list(c(dq1,dq2,dp1,dp2))
    } )
}

# Pass NULL to parms
out2 <- ode(y = ini, times = times, func = rhs, parms = NULL,method="euler")

identical(out, out2)
## [1] TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM