e04xa {NAGFWrappers} R Documentation

## e04xa: Estimate (using numerical differentiation) gradient and/or Hessian of a function

### Description

e04xa computes an approximation to the gradient vector and/or the Hessian matrix for use in conjunction with, or following the use of an optimization function (such as e04uf).

### Usage

```e04xa(msglvl, epsrf, x, mode, objfun, hforw, lwsav, iwsav, rwsav,
n = nrow(x))
```

### Arguments

 `msglvl` integer Must indicate the amount of intermediate output desired (see the printed output description in the Fortran library documentation for a description of the printed output). All output is written on the current advisory message unit (see x04ab). `epsrf` double Must define e_R, which is intended to be a measure of the accuracy with which the problem function F can be computed. The value of e_R should reflect the relative precision of 1 + abs(F(x)), i.e., acts as a relative precision when abs(F) is large, and as an absolute precision when abs(F) is small. For example, if F(x) is typically of order 1000 and the first six significant digits are known to be correct, an appropriate value for e_R would be 1.0E-6. `x` double array The point x at which the derivatives are to be computed. `mode` integer Indicates which derivatives are required. mode = 0: The gradient and Hessian diagonal values having supplied the objective function via objfun. mode = 1: The Hessian matrix having supplied both the objective function and gradients via objfun. mode = 2: The gradient values and Hessian matrix having supplied the objective function via objfun. `objfun` function If mode=0, 2, objfun must calculate the objective function; otherwise if mode = 1, objfun must calculate the objective function and the gradients. ```(MODE,OBJF,OBJGRD) = objfun(mode,n,x,nstate) ``` `hforw` double array The initial trial interval for computing the appropriate partial derivative to the jth variable. `lwsav` boolean array `iwsav` integer array `rwsav` double array These arguments are no longer required by e04xa. `n` integer: default = nrow(x) The number n of independent variables.

### Details

R interface to the NAG Fortran routine E04XAF.

### Value

 `MODE` integer Is changed only if you set mode negative in objfun, i.e., you have requested termination of e04xa. `HFORW` double array hforw[j] is the best interval found for computing a forward-difference approximation to the appropriate partial derivative for the jth variable. `OBJF` double The value of the objective function evaluated at the input vector in x. `OBJGRD` double array If mode=0, 2, objgrd[j] contains the best estimate of the first partial derivative for the jth variable. `HCNTRL` double array hcntrl[j] is the best interval found for computing a central-difference approximation to the appropriate partial derivative for the jth variable. `H` double array If mode = 0, the estimated Hessian diagonal elements are contained in the first column of this array. `IWARN` integer iwarn = 0 on successful exit. `INFO` integer array info[j] represents diagnostic information on variable j. (See the Errors section in Fortran library documentation for more details.) `IFAIL` integer ifail =0 unless the function detects an error or a warning has been flagged (see the Errors section in Fortran library documentation).

NAG

### Examples

```
optlist <- list()

ifail <- 0
objfun = function(mode, n, x, nstate) {

objgrd <- as.matrix(mat.or.vec(n, 1))
a <- x + 10 %*% x
b <- x - x
c <- x - 2 %*% x
d <- x - x
objf <- a^2 + 5 %*% b^2 + c^4 + 10 %*% d^4

if (mode == 1) {

objgrd <- 40 %*% x^3 + 2 %*% x - 120 %*% x %*%
x^2 + 120 %*% x %*% x^2 + 20 %*% x -
40 %*% x^3

objgrd <- 200 %*% x + 20 %*% x + 4 %*% x^3 +
48 %*% x %*% x^2 - 24 %*% x %*% x^2 -
32 %*% x^3

objgrd <- 10 %*% x - 10 %*% x - 8 %*% x^3 +
48 %*% x %*% x^2 - 96 %*% x %*% x^2 +
64 %*% x^3

objgrd <- 10 %*% x - 10 %*% x - 40 %*% x^3 +
120 %*% x %*% x^2 - 120 %*% x %*% x^2 +
40 %*% x^3

}
list(MODE = as.integer(mode), OBJF = objf, OBJGRD = as.matrix(objgrd))
}

msglvl <- 0

epsrf <- -1

x <- matrix(c(3, -1, 0, 1), nrow = 4, ncol = 1, byrow = TRUE)

mode <- 0

hforw <- matrix(c(-1, -1, -1, -1), nrow = 4, ncol = 1,
byrow = TRUE)

lwsav <- as.matrix(mat.or.vec(120, 1))

iwsav <- as.matrix(mat.or.vec(610, 1))

rwsav <- as.matrix(mat.or.vec(475, 1))

e04xa(msglvl, epsrf, x, mode, objfun, hforw, lwsav,
iwsav, rwsav)

```

[Package NAGFWrappers version 24.0 Index]