# Syntax

C# |
---|

public static void e04xa( int msglvl, int n, double epsrf, double[] x, ref int mode, E04..::..E04XA_OBJFUN objfun, double[] hforw, out double objf, double[] objgrd, double[] hcntrl, double[,] h, out int iwarn, int[] info, out int ifail ) |

Visual Basic |
---|

Public Shared Sub e04xa ( _ msglvl As Integer, _ n As Integer, _ epsrf As Double, _ x As Double(), _ ByRef mode As Integer, _ objfun As E04..::..E04XA_OBJFUN, _ hforw As Double(), _ <OutAttribute> ByRef objf As Double, _ objgrd As Double(), _ hcntrl As Double(), _ h As Double(,), _ <OutAttribute> ByRef iwarn As Integer, _ info As Integer(), _ <OutAttribute> ByRef ifail As Integer _ ) |

Visual C++ |
---|

public: static void e04xa( int msglvl, int n, double epsrf, array<double>^ x, int% mode, E04..::..E04XA_OBJFUN^ objfun, array<double>^ hforw, [OutAttribute] double% objf, array<double>^ objgrd, array<double>^ hcntrl, array<double,2>^ h, [OutAttribute] int% iwarn, array<int>^ info, [OutAttribute] int% ifail ) |

F# |
---|

static member e04xa : msglvl : int * n : int * epsrf : float * x : float[] * mode : int byref * objfun : E04..::..E04XA_OBJFUN * hforw : float[] * objf : float byref * objgrd : float[] * hcntrl : float[] * h : float[,] * iwarn : int byref * info : int[] * ifail : int byref -> unit |

#### Parameters

- msglvl
- Type: System..::..Int32
*On entry*: must indicate the amount of intermediate output desired (see [Description of the Printed Output] for a description of the printed output). All output is written on the current advisory message unit (see (X04ABF not in this release)).**Value****Definition**0 No printout 1 A summary is printed out for each variable plus any warning messages. Other Values other than $0$ and $1$ should normally be used **only**at the direction of NAG.

- n
- Type: System..::..Int32
*On entry*: the number $n$ of independent variables.*Constraint*: ${\mathbf{n}}\ge 1$.

- epsrf
- Type: System..::..Double
*On entry*: 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+\left|F\left(x\right)\right|$, i.e., acts as a relative precision when $\left|F\right|$ is large, and as an absolute precision when $\left|F\right|$ is small. For example, if $F\left(x\right)$ is typically of order $1000$ and the first six significant digits are known to be correct, an appropriate value for ${e}_{R}$ would be $\text{1.0E\u22126}$.A discussion of epsrf is given in Chapter 8 of Gill et al. (1981). If epsrf is either too small or too large on entry a warning will be printed if ${\mathbf{msglvl}}=1$, the parameter iwarn set to the appropriate value on exit and e04xa will use a default value of ${e}_{M}^{0.9}$, where ${e}_{M}$ is the machine precision.If ${\mathbf{epsrf}}\le 0.0$ on entry then e04xa will use the default value internally. The default value will be appropriate for most simple functions that are computed with full accuracy.

- x
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On entry*: the point $x$ at which the derivatives are to be computed.

- mode
- Type: System..::..Int32%
*On entry*: indicates which derivatives are required.- ${\mathbf{mode}}=0$
- The gradient and Hessian diagonal values having supplied the objective function via objfun.
- ${\mathbf{mode}}=1$
- The Hessian matrix having supplied both the objective function and gradients via objfun.
- ${\mathbf{mode}}=2$
- The gradient values and Hessian matrix having supplied the objective function via objfun.

- objfun
- Type: NagLibrary..::..E04..::..E04XA_OBJFUNIf ${\mathbf{mode}}=0$ or $2$, objfun must calculate the objective function; otherwise if ${\mathbf{mode}}=1$, objfun must calculate the objective function and the gradients.
A delegate of type E04XA_OBJFUN.

- hforw
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On entry*: the initial trial interval for computing the appropriate partial derivative to the $j$th variable.If ${\mathbf{hforw}}\left[j-1\right]\le 0.0$, then the initial trial interval is computed by e04xa (see [Description]).*On exit*: ${\mathbf{hforw}}\left[j-1\right]$ is the best interval found for computing a forward-difference approximation to the appropriate partial derivative for the $j$th variable.

- objf
- Type: System..::..Double%
*On exit*: the value of the objective function evaluated at the input vector in x.

- objgrd
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On exit*: if ${\mathbf{mode}}=0$ or $2$, ${\mathbf{objgrd}}\left[j-1\right]$ contains the best estimate of the first partial derivative for the $j$th variable.If ${\mathbf{mode}}=1$, ${\mathbf{objgrd}}\left[j-1\right]$ contains the first partial derivative for the $j$th variable evaluated at the input vector in x.

- hcntrl
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On exit*: ${\mathbf{hcntrl}}\left[j-1\right]$ is the best interval found for computing a central-difference approximation to the appropriate partial derivative for the $j$th variable.

- h
- Type: array<System..::..Double,2>[,](,)[,][,]An array of size [dim1, dim2]
**Note:**dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$**Note:**the second dimension of the array h must be at least $1$ if ${\mathbf{mode}}=0$ and at least ${\mathbf{n}}$ if ${\mathbf{mode}}=1$ or $2$.*On exit*: if ${\mathbf{mode}}=0$, the estimated Hessian diagonal elements are contained in the first column of this array.If ${\mathbf{mode}}=1$ or $2$, the estimated Hessian matrix is contained in the leading $n$ by $n$ part of this array.

- iwarn
- Type: System..::..Int32%
*On exit*: ${\mathbf{iwarn}}=0$ on successful exit.If the value of epsrf on entry is too small or too large then iwarn is set to $1$ or $2$ respectively on exit and the default value for epsrf is used within e04xa.If ${\mathbf{msglvl}}>0$ then warnings will be printed if epsrf is too small or too large.

- info
- Type: array<System..::..Int32>[]()[][]An array of size [n]
*On exit*: ${\mathbf{info}}\left[j-1\right]$ represents diagnostic information on variable $j$. (See [Error Indicators and Warnings] for more details.)

- ifail
- Type: System..::..Int32%
*On exit*: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

e04xa is similar to routine FDCALC described in Gill et al. (1983a). It should be noted that this method aims to compute sufficiently accurate estimates of the derivatives for use with an optimization algorithm. If you require more accurate estimates you should refer to (D04 not in this release).

e04xa computes finite difference approximations to the gradient vector and the Hessian matrix for a given function. The simplest approximation involves the forward-difference formula, in which the derivative ${f}^{\prime}\left(x\right)$ of a univariate function $f\left(x\right)$ is approximated by the quantity

for some interval $h>0$, where the subscript "F" denotes ‘forward-difference’ (see Gill et al. (1983b)).

$${\rho}_{F}\left(f,h\right)=\frac{f\left(x+h\right)-f\left(x\right)}{h}$$ |

To summarise the procedure used by e04xa (for the case when the objective function is available and you require estimates of gradient values and Hessian matrix diagonal values, i.e., ${\mathbf{mode}}=0$) consider a univariate function $f$ at the point $x$. (In order to obtain the gradient of a multivariate function $F\left(x\right)$, where $x$ is an $n$-vector, the procedure is applied to each component of $x$, keeping the other components fixed.) Roughly speaking, the method is based on the fact that the bound on the relative truncation error in the forward-difference approximation tends to be an increasing function of $h$, while the relative condition error bound is generally a decreasing function of $h$, hence changes in $h$ will tend to have opposite effects on these errors (see Gill et al. (1983b)).

The ‘best’ interval $h$ is given by

where $\Phi $ is an estimate of ${f}^{\prime \prime}\left(x\right)$, and ${e}_{R}$ is an estimate of the relative error associated with computing the function (see Chapter 8 of Gill et al. (1981)). Given an interval $h$, $\Phi $ is defined by the second-order approximation

The decision as to whether a given value of $\Phi $ is acceptable involves $\hat{c}\left(\Phi \right)$, the following bound on the relative condition error in $\Phi $:

(When $\Phi $ is zero, $\hat{c}\left(\Phi \right)$ is taken as an arbitrary large number.)

$${h}_{F}=2\sqrt{\frac{\left(1+\left|f\left(x\right)\right|\right){e}_{R}}{\left|\Phi \right|}}$$ | (1) |

$$\Phi =\frac{f\left(x+h\right)-2f\left(x\right)+f\left(x-h\right)}{{h}^{2}}\text{.}$$ |

$$\hat{c}\left(\Phi \right)=\frac{4{e}_{R}\left(1+\left|f\right|\right)}{{h}^{2}\left|\Phi \right|}$$ |

The procedure selects the interval ${h}_{\varphi}$ (to be used in computing $\Phi $) from a sequence of trial intervals $\left({h}_{k}\right)$. The initial trial interval is taken as $10\stackrel{-}{h}$, where

unless you specify the initial value to be used.

$$\stackrel{-}{h}=2\left(1+\left|x\right|\right)\sqrt{{e}_{R}}$$ |

The value of $\hat{c}\left(\Phi \right)$ for a trial value ${h}_{k}$ is defined as ‘acceptable’ if it lies in the interval $\left[0.001,0.1\right]$. In this case ${h}_{\varphi}$ is taken as ${h}_{k}$, and the current value of $\Phi $ is used to compute ${h}_{F}$ from (1). If $\hat{c}\left(\Phi \right)$ is unacceptable, the next trial interval is chosen so that the relative condition error bound will either decrease or increase, as required. If the bound on the relative condition error is too large, a larger interval is used as the next trial value in an attempt to reduce the condition error bound. On the other hand, if the relative condition error bound is too small, ${h}_{k}$ is reduced.

The procedure will fail to produce an acceptable value of $\hat{c}\left(\Phi \right)$ in two situations. Firstly, if ${f}^{\prime \prime}\left(x\right)$ is extremely small, then $\hat{c}\left(\Phi \right)$ may never become small, even for a very large value of the interval. Alternatively, $\hat{c}\left(\Phi \right)$ may never exceed $0.001$, even for a very small value of the interval. This usually implies that ${f}^{\prime \prime}\left(x\right)$ is extremely large, and occurs most often near a singularity.

As a check on the validity of the estimated first derivative, the procedure provides a comparison of the forward-difference approximation computed with ${h}_{F}$ (as above) and the central-difference approximation computed with ${h}_{\varphi}$. Using the central-difference formula the first derivative can be approximated by

where $h>0$. If the values ${h}_{F}$ and ${h}_{\varphi}$ do not display some agreement, neither can be considered reliable.

$${\rho}_{c}\left(f,h\right)=\frac{f\left(x+h\right)-f\left(x-h\right)}{2h}$$ |

When both function and gradients are available and you require the Hessian matrix (i.e., ${\mathbf{mode}}=1$) e04xa follows a similar procedure to the case above with the exception that the gradient function $g\left(x\right)$ is substituted for the objective function and so the forward-difference interval for the first derivative of $g\left(x\right)$ with respect to variable ${x}_{j}$ is computed. The $j$th column of the approximate Hessian matrix is then defined as in Chapter 2 of Gill et al. (1981), by

where ${h}_{j}$ is the best forward-difference interval associated with the $j$th component of $g$ and ${e}_{j}$ is the vector with unity in the $j$th position and zeros elsewhere.

$$\frac{g\left(x+{h}_{j}{e}_{j}\right)-g\left(x\right)}{{h}_{j}}$$ |

When only the objective function is available and you require the gradients and Hessian matrix (i.e., ${\mathbf{mode}}=2$) e04xa again follows the same procedure as the case for ${\mathbf{mode}}=0$ except that this time the value of $\hat{c}\left(\Phi \right)$ for a trial value ${h}_{k}$ is defined as acceptable if it lies in the interval $\left[0.0001,0.01\right]$ and the initial trial interval is taken as

The approximate Hessian matrix $G$ is then defined as in Chapter 2 of Gill et al. (1981), by

$$\stackrel{-}{h}=2\left(1+\left|x\right|\right)\sqrt[4]{{e}_{R}}\text{.}$$ |

$${G}_{ij}\left(x\right)=\frac{1}{{h}_{i}{h}_{j}}\left(f\left(x+{h}_{i}{e}_{i}+{h}_{j}{e}_{j}\right)-f\left(x+{h}_{i}{e}_{i}\right)-f\left(x+{h}_{j}{e}_{j}\right)+f\left(x\right)\right)\text{.}$$ |

# References

Gill P E, Murray W, Saunders M A and Wright M H (1983a) Documentation for FDCALC and FDCORE

*Technical Report SOL*83–6 Stanford UniversityGill P E, Murray W, Saunders M A and Wright M H (1983b) Computing forward-difference intervals for numerical optimization

*SIAM J. Sci. Statist. Comput.***4**310–321Gill P E, Murray W and Wright M H (1981)

*Practical Optimization*Academic Press# Error Indicators and Warnings

On exit from e04xa both diagnostic parameters info and ifail should be tested. ifail represents an overall diagnostic indicator, whereas the integer array info represents diagnostic information on each variable.

Errors or warnings detected by the method:

Some error messages may refer to parameters that are dropped from this interface
(LDH) In these
cases, an error in another parameter has usually caused an incorrect value to be inferred.

- ${\mathbf{ifail}}<0$

- ${\mathbf{ifail}}=1$
- On entry, one or more of the following conditions are satisfied: ${\mathbf{n}}<1$, ${\mathbf{ldh}}<{\mathbf{n}}\text{ or}{\mathbf{mode}}$ is invalid.

- ${\mathbf{ifail}}=2$

- ${\mathbf{ifail}}=-9000$
- An error occured, see message report.
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-4000$
- Invalid dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-8000$
- Negative dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$

Diagnostic information returned via info is as follows:

Some error messages may refer to parameters that are dropped from this interface
(LDH) In these
cases, an error in another parameter has usually caused an incorrect value to be inferred.

- ${\mathbf{info}}=1$
- The appropriate function appears to be constant. ${\mathbf{hforw}}\left[i-1\right]$ is set to the initial trial interval value (see [Description]) corresponding to a well-scaled problem and Error est. in the printed output is set to zero. This value occurs when the estimated relative condition error in the first derivative approximation is unacceptably large for every value of the finite difference interval. If this happens when the function is not constant the initial interval may be too small; in this case, it may be worthwhile to rerun e04xa with larger initial trial interval values supplied in hforw (see [Description]). This error may also occur if the function evaluation includes an inordinately large constant term or if epsrf is too large.

- ${\mathbf{info}}=2$
- The appropriate function appears to be linear or odd. ${\mathbf{hforw}}\left[i-1\right]$ is set to the smallest interval with acceptable bounds on the relative condition error in the forward- and backward-difference estimates. In this case, the estimated relative condition error in the second derivative approximation remained large for every trial interval, but the estimated error in the first derivative approximation was acceptable for at least one interval. If the function is not linear or odd the relative condition error in the second derivative may be decreasing very slowly, it may be worthwhile to rerun e04xa with larger initial trial interval values supplied in hforw (see [Description]).

- ${\mathbf{info}}=3$
- The second derivative of the appropriate function appears to be so large that it cannot be reliably estimated (i.e., near a singularity). ${\mathbf{hforw}}\left[i-1\right]$ is set to the smallest trial interval.This value occurs when the relative condition error estimate in the second derivative remained very small for every trial interval.If the second derivative is not large the relative condition error in the second derivative may be increasing very slowly. It may be worthwhile to rerun e04xa with smaller initial trial interval values supplied in hforw (see [Description]). This error may also occur when the given value of epsrf is not a good estimate of a bound on the absolute error in the appropriate function (i.e., epsrf is too small).

- ${\mathbf{info}}=4$
- The algorithm terminated with an apparently acceptable estimate of the second derivative. However the forward-difference estimates of the appropriate first derivatives (computed with the final estimate of the ‘optimal’ forward-difference interval) and the central difference estimates (computed with the interval used to compute the final estimate of the second derivative) do not agree to half a decimal place. The usual reason that the forward- and central-difference estimates fail to agree is that the first derivative is small.If the first derivative is not small, it may be helpful to execute the procedure at a different point.

- ${\mathbf{ifail}}=-9000$
- An error occured, see message report.
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-4000$
- Invalid dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-8000$
- Negative dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$

# Accuracy

If ${\mathbf{ifail}}={0}$ on exit the algorithm terminated successfully, i.e., the forward-difference estimates of the appropriate first derivatives (computed with the final estimate of the ‘optimal’ forward-difference interval ${h}_{F}$) and the central-difference estimates (computed with the interval ${h}_{\varphi}$ used to compute the final estimate of the second derivative) agree to at least half a decimal place.

In short word length implementations when computing the full Hessian matrix given function values only (i.e., ${\mathbf{mode}}=2$) the elements of the computed Hessian will have at best $1$ to $2$ figures of accuracy.

# Parallelism and Performance

None.

# Further Comments

To evaluate an acceptable set of finite difference intervals for a well-scaled problem, the method will require around two function evaluations per variable; in a badly scaled problem however, as many as six function evaluations per variable may be needed.

If you request the full Hessian matrix supplying both function and gradients (i.e., ${\mathbf{mode}}=1$) or function only (i.e., ${\mathbf{mode}}=2$) then a further n or $3\times {\mathbf{n}}\times \left({\mathbf{n}}+1\right)/2$ function evaluations respectively are required.

# Description of the Printed Output

The following is a description of the printed output from e04xa as controlled by the parameter msglvl.

Output when ${\mathbf{msglvl}}=1$ is as follows:

J | number of variable for which the difference interval has been computed. |

$\mathtt{X}\left(j\right)$ | $j$th variable of $x$ as set by you. |

F. dif. int. | the best interval found for computing a forward-difference approximation to the appropriate partial derivative with respect to the $j$th variable. |

C. dif. int. | the best interval found for computing a central-difference approximation to the appropriate partial derivative with respect to the $j$th variable. |

Error est. | a bound on the estimated error in the final forward-difference approximation. When ${\mathbf{info}}\left[j-1\right]=1$, Error est. is set to zero. |

Grad. est. | best estimate of the first partial derivative with respect to the $j$th variable. |

Hess diag est. | best estimate of the second partial derivative with respect to the $j$th variable. |

fun evals. | the number of function evaluations used to compute the final difference intervals for the $j$th variable. |

$\mathtt{info}\left(j\right)$ | the value of info for the $j$th variable. |

# Example

This example computes the gradient vector and the Hessian matrix of the following function:

at the point $\left(3,-1,0,1\right)$.

$$F\left(x\right)={\left({x}_{1}+10{x}_{2}\right)}^{2}+5{\left({x}_{3}-{x}_{4}\right)}^{2}+{\left({x}_{2}-2{x}_{3}\right)}^{4}+10{\left({x}_{1}-{x}_{4}\right)}^{4}$$ |