e04hc {NAGFWrappers}R Documentation

e04hc: Check user's function for calculating first derivatives of function

Description

e04hc checks that a function for evaluating an objective function and its first derivatives produces derivative values which are consistent with the function values calculated.

Usage

e04hc(funct, x,
      n = nrow(x))

Arguments

funct

function

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

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

x

double array

x[j]

for j=1 . . . n, must be set to 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 preferable that no two elements of x should be the same.

n

integer: default = nrow(x)

The number n of independent variables in the objective function.

Details

R interface to the NAG Fortran routine E04HCF.

Value

F

double

Unless you set iflag negative in the first call of funct, f contains the value of the objective function F(x) at the point given by you in x.

G

double array

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

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/e04hcf.pdf

Examples


ifail <- 0
funct = function(iflag, n, xc) {
    
    gc <- as.matrix(mat.or.vec(n, 1))
    fc <- 0
    
    if (iflag != 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
        
    }
    
    if (iflag != 0) {
        
        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))
}

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




e04hc(funct, x) 


[Package NAGFWrappers version 24.0 Index]