0

I'm currently trying to calculate finite differences in R using the fderiv method from the pracma library. I've developed the method to identify the Lorenz derivatives below:

library(deSolve)

parameters <- c(s = 10, r = 28, b = 8/3) # Lorenz Parameters: sigma, rho, beta
state <- c(X = -8, Y = 7, Z = 27) # Initial State

# Lorenz Function used to generate Lorenz Derivatives
lorenz <- function(t, state, parameters) {
    with(as.list(c(state, parameters)), {
      dX <- s * (Y - X)
      dY <- X * (r - Z) - Y
      dZ <- X * Y - b * Z
      list(c(dX, dY, dZ))
    })
  }

times <- seq(0.01, 100, by = 0.01)
# ODE45 used to determine Lorenz Matrix
out <- ode(y = state, times = times, func = lorenz, parms = parameters)

However, I'm struggling to develop the first or second order differences from this function. When I use fderiv(Vectorize(lorenz), state = state, parameters = parameters, x = times, n = 1, "forward") I get the error Error in eval(substitute(expr), data, enclos = parent.frame()) : object 'Y' not found. I was hoping for insight on how I can develop these finite differences with fderiv.

Thanks in advance.

AW27
  • 481
  • 3
  • 15
  • You might start with `traceback()` to see what function or call is throwing that error – Carl Witthoft Oct 05 '20 at 18:21
  • Ìf you put some `cat` functions in `lorenz`, you will see that `fderiv` passes only parts of the parameter and state vectors down to the function. – tpetzoldt Oct 05 '20 at 19:41
  • 1
    Your `lorenz` function returns a list, not a vector. That makes it inappropriate for most ODE solvers and also for numerical derivatives. `lorenz` itself computes derivatives, what do you need `fderiv` for? BTW, for numerical gradients it is better to apply `grad` from the *numderiv* or *pracma* packages. – Hans W. Oct 11 '20 at 11:10

0 Answers0