Learn R Programming

optextras (version 2013-10.27)

grnd: A reorganization of the call to numDeriv grad() function.

Description

Provides a wrapper for the numDeriv approximation to the gradient of a user supplied objective function userfn.

Usage

grnd(par, userfn, ...)

Arguments

par
A vector of parameters to the user-supplied function fn
userfn
A user-supplied function
...
Other data needed to evaluate the user function.

Value

  • grnd returns an approximation to the gradient of the function userfn

encoding

UTF-8

concept

  • minimization
  • maximization

Details

The Richardson method is used in this routine.

Examples

Run this code
cat("Example of use of grnd
")
require(numDeriv)
myfn<-function(xx, shift=100){
    ii<-1:length(xx)
    result<-shift+sum(xx^ii)
}
xx<-c(1,2,3,4)
ii<-1:length(xx)
print(xx)
gn<-grnd(xx,myfn, shift=0)
print(gn)
ga<-ii*xx^(ii-1)
cat("compare to
")
print(ga)

Run the code above in your browser using DataLab