# ODE system and gradients for Fitzhugh-Nagumo equations: fnmodelODE, fnmodelDx, fnmodelDtheta
# Example of incorrect gradient with respect to parameters theta
fnmodelDthetaWrong <- function(theta, x, tvec) {
resultDtheta <- array(0, c(nrow(x), length(theta), ncol(x)))
V = x[, 1]
R = x[, 2]
resultDtheta[, 3, 1] = V - V^3 / 3.0 - R
resultDtheta[, 1, 2] = 1.0 / theta[3]
resultDtheta[, 2, 2] = -R / theta[3]
resultDtheta[, 3, 2] = 1.0 / (theta[3]^2) * (V - theta[1] + theta[2] * R)
resultDtheta
}
# Sample data for testing gradient correctness
data(FNdat)
# Correct gradients
testDynamicalModel(fnmodelODE, fnmodelDx, fnmodelDtheta,
"FN equations", FNdat[, c("V", "R")], c(.5, .6, 2), FNdat$time)
# Incorrect theta gradient (test fails)
testDynamicalModel(fnmodelODE, fnmodelDx, fnmodelDthetaWrong,
"FN equations", FNdat[, c("V", "R")], c(.5, .6, 2), FNdat$time)
Run the code above in your browser using DataLab