Numerical Differentiation in R

385 views Asked by At

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.

0

There are 0 answers