Finite-difference approximation of the gradient of a scalar function.
Parameters: | xk : array_like
f : callable
epsilon : array_like
*args : args, optional
|
---|---|
Returns: | grad : ndarray
|
See also
Notes
The function gradient is determined by the forward finite difference formula:
f(xk[i] + epsilon[i]) - f(xk[i])
f'[i] = ---------------------------------
epsilon[i]
The main use of approx_fprime is in scalar function optimizers like fmin_bfgs, to determine numerically the Jacobian of a function.
Examples
>>> from scipy import optimize
>>> def func(x, c0, c1):
... "Coordinate vector `x` should be an array of size two."
... return c0 * x[0]**2 + c1*x[1]**2
>>> x = np.ones(2)
>>> c0, c1 = (1, 200)
>>> eps = np.sqrt(np.finfo(np.float).eps)
>>> optimize.approx_fprime(x, func, [eps, np.sqrt(200) * eps], c0, c1)
array([ 2. , 400.00004198])