gradp
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.
Format
g = gradp(&f,x0);
Input
&f
a pointer to a vector-valued function (f:Kx1 - > Nx1) defined as a procedure. It is acceptable for f(x) to have been defined in terms of global arguments in addition to x, and thus f can return an Nx1 vector:
proc f(x);
retp( exp(x.*b) );
endp;
x0
Kx1 vector of points at which to compute gradient.
Output
g
NxK matrix containing the gradients of f with respect to the variable x at x0.
Remarks
gradp will return a row for every row that is returned by f. For instance, if f 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 f. Thus, for instance, gradp can be used to compute the Jacobian matrix of a set of equations.
Example
proc myfunc(x);
retp( x.*2 .* exp( x.*x./3 ) );
endp;
x0 = 2.5|3.0|3.5;
y = gradp(&myfunc,x0);
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.