NAG Library Routine Document
e04yaf
(lsq_check_deriv)
1
Purpose
e04yaf checks that a user-supplied subroutine for evaluating a vector of functions and the matrix of their first derivatives produces derivative values which are consistent with the function values calculated.
2
Specification
Fortran Interface
Subroutine e04yaf ( |
m,
n,
lsqfun,
x,
fvec,
fjac,
ldfjac,
iw,
liw,
w,
lw,
ifail) |
Integer, Intent (In) | :: |
m,
n,
ldfjac,
liw,
lw | Integer, Intent (Inout) | :: |
iw(liw),
ifail | Real (Kind=nag_wp), Intent (In) | :: |
x(n) | Real (Kind=nag_wp), Intent (Inout) | :: |
fjac(ldfjac,n),
w(lw) | Real (Kind=nag_wp), Intent (Out) | :: |
fvec(m) | External | :: |
lsqfun |
|
C Header Interface
#include nagmk26.h
void |
e04yaf_ (
const Integer *m,
const Integer *n,
void (NAG_CALL *lsqfun)(
Integer *iflag,
const Integer *m,
const Integer *n,
const double xc[],
double fvec[],
double fjac[],
const Integer *ldfjac,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw),
const double x[],
double fvec[],
double fjac[],
const Integer *ldfjac,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw,
Integer *ifail) |
|
3
Description
Routines for minimizing a sum of squares of
nonlinear functions (or ‘residuals’),
, for
and
, may require you to supply a subroutine to evaluate the
and their first derivatives.
e04yaf checks the derivatives calculated by such user-supplied subroutines, e.g., routines of the form required for
e04gbf,
e04gdf and
e04hef. As well as the routine to be checked (
lsqfun), you must supply a point
at which the check will be made.
e04yaf is essentially identical to CHKLSJ in the NPL Algorithms Library.
e04yaf first calls
lsqfun to evaluate the
and their first derivatives, and uses these to calculate the sum of squares
,
and its first derivatives
, for
. The components of
along two orthogonal directions (defined by unit vectors
and
, say) are then calculated; these will be
and
respectively. The same components are also estimated by finite differences, giving quantities
where
is a small positive scalar. If the relative difference between
and
or between
and
is judged too large, an error indicator is set.
4
References
None.
5
Arguments
- 1: – IntegerInput
- 2: – IntegerInput
-
On entry: the number of residuals, , and the number of variables, .
Constraint:
.
- 3: – Subroutine, supplied by the user.External Procedure
-
lsqfun must calculate the vector of values
and their first derivatives
at any point
. (The minimization routines mentioned in
Section 3 give you the option of resetting an argument to terminate immediately.
e04yaf will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
The specification of
lsqfun is:
Fortran Interface
Subroutine lsqfun ( |
iflag,
m,
n,
xc,
fvec,
fjac,
ldfjac,
iw,
liw,
w,
lw) |
Integer, Intent (In) | :: |
m,
n,
ldfjac,
liw,
lw | Integer, Intent (Inout) | :: |
iflag,
iw(liw) | Real (Kind=nag_wp), Intent (In) | :: |
xc(n) | Real (Kind=nag_wp), Intent (Inout) | :: |
fjac(ldfjac,n),
w(lw) | Real (Kind=nag_wp), Intent (Out) | :: |
fvec(m) |
|
C Header Interface
#include nagmk26.h
void |
lsqfun (
Integer *iflag,
const Integer *m,
const Integer *n,
const double xc[],
double fvec[],
double fjac[],
const Integer *ldfjac,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw) |
|
- 1: – IntegerInput/Output
-
On entry: to
lsqfun,
iflag will be set to
.
On exit: if you reset
iflag to some negative number in
lsqfun and return control to
e04yaf, the routine will terminate immediately with
ifail set to your setting of
iflag.
- 2: – IntegerInput
-
On entry: the numbers of residuals.
- 3: – IntegerInput
-
On entry: the numbers of variables.
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry: , the point at which the values of the and the are required.
- 5: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
iflag is reset to a negative number,
must contain the value of
at the point
, for
.
- 6: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
iflag is reset to a negative number,
must contain the value of
at the point
, for
and
.
- 7: – IntegerInput
-
On entry: the first dimension of the array
fjac as declared in the (sub)program from which
e04yaf is called.
- 8: – Integer arrayWorkspace
- 9: – IntegerInput
- 10: – Real (Kind=nag_wp) arrayWorkspace
- 11: – IntegerInput
-
These arguments are present so that
lsqfun will be of the form required by the minimization routines mentioned in
Section 3.
lsqfun is called with the same arguments
iw,
liw,
w,
lw as in the call to
e04yaf. If the recommendation in the minimization routine document is followed, you will have no reason to examine or change the elements of
iw or
w. In any case,
lsqfun must not change the first
elements of
w.
lsqfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
e04yaf is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: lsqfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
e04yaf. If your code inadvertently
does return any NaNs or infinities,
e04yaf is likely to produce unexpected results.
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry:
, for
, must be set to the coordinates of a suitable point at which to check the derivatives calculated by
lsqfun. ‘Obvious’ settings, such as
or
, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors can go undetected. For a similar reason, it is preferable that no two elements of
x should have the same value.
- 5: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless you set
iflag negative in the first call of
lsqfun,
contains the value of
at the point supplied by you in
x, for
.
- 6: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless you set
iflag negative in the first call of
lsqfun,
contains the value of the first derivative
at the point given in
x, as calculated by
lsqfun, for
and
.
- 7: – IntegerInput
-
On entry: the first dimension of the array
fjac as declared in the (sub)program from which
e04yaf is called.
Constraint:
.
- 8: – Integer arrayCommunication Array
-
This array appears in the argument list purely so that, if
e04yaf is called by another library routine, the library routine can pass quantities to
lsqfun via
iw.
iw is not examined or changed by
e04yaf. In general you must provide an array
iw, but are advised not to use it.
- 9: – IntegerInput
-
On entry: the dimension of the array
iw as declared in the (sub)program from which
e04yaf is called.
Constraint:
.
- 10: – Real (Kind=nag_wp) arrayCommunication Array
- 11: – IntegerInput
-
On entry: the dimension of the array
w as declared in the (sub)program from which
e04yaf is called.
Constraint:
.
- 12: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if
on exit, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Note: e04yaf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
-
A negative value of
ifail indicates an exit from
e04yaf because you have set
iflag negative in
lsqfun. The setting of
ifail will be the same as your setting of
iflag. The check on
lsqfun will not have been completed.
-
On entry, | , |
or | , |
or | , |
or | , |
or | . |
-
You should check carefully the derivation and programming of expressions for the
, because it is very unlikely that
lsqfun is calculating them correctly.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
ifail is set to
if
for
. (See
Section 3 for definitions of the quantities involved.) The scalar
is set equal to
, where
is the
machine precision as given by
x02ajf.
8
Parallelism and Performance
e04yaf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
e04yaf calls
lsqfun three times.
Before using
e04yaf to check the calculation of the first derivatives, you should be confident that
lsqfun is calculating the residuals correctly.
e04yaf only checks the derivatives calculated by a user-supplied routine when
. So, if
lsqfun is intended for use in conjunction with a minimization routine which may set
iflag to
, you must check that, for given settings of the
,
lsqfun produces the same values for the
when
iflag is set to
as when
iflag is set to
.
10
Example
Suppose that it is intended to use
e04gbf or
e04gdf to find least squares estimates of
and
in the model
using the
sets of data given in the following table.
The following program could be used to check the first derivatives calculated by
lsqfun. (The tests of whether
or
in
lsqfun are present ready for when
lsqfun is called by
e04gbf or
e04gdf.
e04yaf will always call
lsqfun with
iflag set to 2.)
10.1
Program Text
Program Text (e04yafe.f90)
10.2
Program Data
Program Data (e04yafe.d)
10.3
Program Results
Program Results (e04yafe.r)