Example
XEGnum23.xpl
approximates the first derivative of
at
by the difference with
a step
. It shows Figure 7.12 and the following output:
Contents of string [1,] "Gradient - analytic computation: 0.030000" Contents of string [1,] "Gradient - numeric computation : 0.032500"
This example uses a quantlet
|
implementing the symmetric difference approximation of gradient of fname at a given point x0. The input parameter fname should be a string containing a name of a function whose gradient is to be computed; x0 is a vector. One can influence the precision of the approximation by setting the optional parameter h--step of the symmetric difference; the step h can be set separately for each component--input a vector of steps in this case--or it can be the same for every component--inputing a single scalar is then enough. The output parameter grad contains the computed gradient.
Example
XEGnum15.xpl
computes the gradient of
at point
. As one can easily
see,
and
:
Contents of grad [1,] -1 [2,] 4
If you need to compute the gradient of a given function more precisely, use the following quantlet:
|
It calls iteratively the quantlet
nmgraddiff
with various values of parameter h and
extrapolates the results to obtain the limit value for
. Ridder's polynomial extrapolation is used.
The quantlet
nmgraditer
is used the same way as
nmgraddiff
. The output parameter z consists
of the computed gradient z.grad, a vector of estimated errors of partial derivatives z.err and
a vector z.hfin of stepsizes h is the last iteration.
nmgraditer
gives
very precise results but the iterative process is more time-consuming, of course. Hence, use
nmgraddiff
for
less accurate but quick results (e.g., in another iterative method) and
nmgraditer
for precise
but slower computation.
nmgraditer
is a fact that it computes also an estimated error of the computed gradient.
In case of oscillating functions, it is advisable to compare the error with a computed value of the gradient: if the error
estimate is relatively high, the initial stepsize should be decreased.
Table 7.1 generated by example
XEGnum16.xpl
compares the results of
several gradient evaluations for
at
(see Fig. 7.2). The gradient was computed analytically using the formula
nmgraddiff
and
nmgraditer
. The table summarizes
step
nmgraddiff
, initial and final step used in
nmgraditer
, the computed
gradient and an error estimated by
nmgraditer
.
|
computes not only the Jacobian of a vector function; it can compute the gradients
of components of a vector function fname even when
.
Input the name of the function(s) whose gradients should be computed as a string or a vector of strings fname.
The gradients will be computed at a point x0, using the quantlet
graddiff
when the parameter
iter is equal to zero or not given, or using
graditer
otherwise--see Remark 7.9.
In both cases, h is an input parameter
for gradient-computing quantlet. The rows of output matrix jacobi contain the gradients of respective
components of fname.
Example
XEGnum18.xpl
computes the Jacobian of a function
defined as
at a point
.
Contents of jacobi [1,] -1 4 [2,] 1 -1
![]() |
|||
Let us suppose that the partial derivatives of the second order of
are continuous;
then
for all
.
The non-diagonal elements of the Hessian contain the mixed partial derivatives of the second order that can be approximated
by the difference
The following quantlet can be used for numerical approximation of the Hessian:
|
The input parameter
fname is a string with a name of the function, x0 is a point (vector) at which the Hessian is to be
computed. The optional parameter h is a stepsize
used in the approximation; it can influence the
precision of the approximation. h can be either a vector or a scalar; in the first case,
-th component of
h is a step size for the difference in the
-th variable, in the latter case, the same value h is
used for all variables.
Example
XEGnum19.xpl
computes the Hessian of
at a point
:
Contents of hess [1,] 0.5 -0.5 [2,] -0.5 0.5
The quantlet
|
restricts the function fname to a line:
.
In the context of optimization,
the main goal of a restriction of a function to a line is to get a function of only one variable. Therefore,
given a function
, we construct a new one-dimensional function
defined as
.
A variable t is the only parameter of
. However, to be able to compute the values of
,
the values of x0 and direc have to be given.
They should be stored in the global variables nmfunc1dx and nmfunc1dd before calling
nmfunc1d
.
The global variable nmfunc1dfunc should be a string with the name of a function computing
.
The resulting value
is returned in an output parameter ft.
Example
XEGnum20.xpl
evaluates
restricted to a line
at
and produces the following result:
Contents of ft [1,] 7
The quantlet
|
computes the derivative of a function fname restricted to a line using the formula
nmfunc1d
, it has only one input parameter t. The values x0 and direc
are taken from the global variables nmfder1dx and nmfder1dd which are set before calling
nmfder1d
. The global variable nmfder1dfunc should be a string with a name of a function to be
restricted. If a function computing the gradient of
nmgraddiff
:
if nmfder1dfder is left empty, the default value of a step
nmgraddiff
or
one can set nmfder1dfder to a value of
nmgraddiff
.
Example
XEGnum21.xpl
computes the derivative of
restricted to a line
at
:
Contents of fdert [1,] 12