gradp, gradcplx

Purpose

Computes the gradient vector or matrix (Jacobian) of a vector-valued function that has been defined in a procedure. Single-sided (forward difference) gradients are computed. gradcplx() allows for complex arguments.

Format

g = gradp(&fct, x0)
g = gradcplx(&fct, x0)
Parameters:

&fct – a pointer to a vector-valued function (fct: \(Kx1 → Nx1\)) defined as a procedure. It is acceptable for \(fct(x)\) to have been defined in terms of global arguments in addition to x, and thus fct can return an Nx1 vector:

proc fct(x);
    retp( exp(x.*b));
endp;
Parameters:

x0 (Kx1 vector) – points at which to compute gradient

Returns:

g (NxK matrix) – containing the gradients of fct with respect to the variable x at x0.

Examples

proc myfunc(x);
   retp(x .* 2 .* exp( x .* x ./ 3 ));
endp;

x0 = 2.5|3.0|3.5;
y = gradp(&myfunc, x0);

After the code above, y is equal to:

82.989017       0.00000000       0.00000000
0.00000000        281.19753       0.00000000
0.00000000       0.00000000        1087.9541
It is a 3x3 matrix because we are passing it 3 arguments and myfunc returns 3 results when we do

that; the off-diagonals are zeros because the cross-derivatives of 3 arguments are 0.

Remarks

gradp() will return a row for every row that is returned by fct. For instance, if fct returns a scalar result, then gradp() will return a 1xK row vector. This allows the same function to be used regardless of N, where N is the number of rows in the result returned by fct. Thus, for instance, gradp() can be used to compute the Jacobian matrix of a set of equations.

Source

gradp.src

See also

Functions hessp(), hesscplx()