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.
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
|
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
Example
XEGnum21.xpl
computes the derivative of
restricted to a line
at
:
Contents of fdert [1,] 12