e04hd {NAGFWrappers}R Documentation

e04hd: Check user's function for calculating second derivatives of function

Description

e04hd checks that a function for calculating second derivatives of an objective function is consistent with a function for calculating the corresponding first derivatives.

Usage

e04hd(funct, h, x, lh,
      n = nrow(x))

Arguments

funct

function

funct must evaluate the function and its first derivatives at a given point. (e04lb gives you the option of resetting arguments of funct to cause the minimization process to terminate immediately. e04hd will also terminate immediately, without finishing the checking process, if the argument in question is reset.)

(IFLAG,FC,GC) = funct(iflag,n,xc)

h

function

h must evaluate the second derivatives of the function at a given point. (As with funct, a argument can be set to cause immediate termination.)

(IFLAG,FHESL,FHESD) = h(iflag,n,xc,lh,fhesd)

x

double array

x[j]

for j=1 . . . n must contain the coordinates of a suitable point at which to check the derivatives calculated by funct. ‘Obvious’ settings, such as 0.0 or 1.0, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. Similarly, it is advisable that no two elements of x should be the same.

lh

integer

n

integer: default = nrow(x)

The number n of independent variables in the objective function.

Details

R interface to the NAG Fortran routine E04HDF.

Value

G

double array

Unless you set iflag negative in the first call of funct, g[j] contains the value of the first derivative ( \partial F)/( \partial x_j) at the point given in x, as calculated by funct for j=1 . . . n.

HESL

double array

Unless you set iflag negative in h, hesl contains the strict lower triangle of the second derivative matrix of F, as evaluated by h at the point given in x, stored by rows.

HESD

double array

Unless you set iflag negative in h, hesd contains the diagonal elements of the second derivative matrix of F, as evaluated by h at the point given in x.

IFAIL

integer

ifail =0

unless the function detects an error or a warning has been flagged (see the Errors section in Fortran library documentation).

Author(s)

NAG

References

http://www.nag.co.uk/numeric/FL/nagdoc_fl23/pdf/E04/e04hdf.pdf

Examples


ifail <- 0
funct = function(iflag, n, xc) {
    
    gc <- as.matrix(mat.or.vec(n, 1))
    fc <- (xc[1] + 10 %*% xc[2])^2 + 5 %*% (xc[3] - xc[4])^2 + 
        (xc[2] - 2 %*% xc[3])^4 + 10 %*% (xc[1] - xc[4])^4
    gc[1] <- 2 %*% (xc[1] + 10 %*% xc[2]) + 40 %*% (xc[1] - xc[4])^3
    gc[2] <- 20 %*% (xc[1] + 10 %*% xc[2]) + 4 %*% (xc[2] - 2 %*% 
        xc[3])^3
    gc[3] <- 10 %*% (xc[3] - xc[4]) - 8 %*% (xc[2] - 2 %*% xc[3])^3
    gc[4] <- 10 %*% (xc[4] - xc[3]) - 40 %*% (xc[1] - xc[4])^3
    list(IFLAG = as.integer(iflag), FC = fc, GC = as.matrix(gc))
}
hess = function(iflag, n, xc, lh, fhesd) {
    
    fhesl <- as.matrix(mat.or.vec(lh, 1))
    fhesd <- as.matrix(mat.or.vec(n, 1))
    fhesd[1] <- 2 + 120 %*% (xc[1] - xc[4])^2
    fhesd[2] <- 200 + 12 %*% (xc[2] - 2 %*% xc[3])^2
    fhesd[3] <- 10 + 48 %*% (xc[2] - 2 %*% xc[3])^2
    fhesd[4] <- 10 + 120 %*% (xc[1] - xc[4])^2
    fhesl[1] <- 20
    fhesl[2] <- 0
    fhesl[3] <- -24 %*% (xc[2] - 2 %*% xc[3])^2
    fhesl[4] <- -120 %*% (xc[1] - xc[4])^2
    fhesl[5] <- 0
    fhesl[6] <- -10
    list(IFLAG = as.integer(iflag), FHESL = as.matrix(fhesl), 
        FHESD = as.matrix(fhesd))
}

x <- matrix(c(1.46, -0.82, 0.57, 1.21), nrow = 4, 
    ncol = 1, byrow = TRUE)



lh <- 6

iw <- matrix(c(0), nrow = 1, ncol = 1, byrow = TRUE)



w <- as.matrix(mat.or.vec(20, 1))

e04hd(funct, hess, x, lh) 


[Package NAGFWrappers version 24.0 Index]