3% [df] = num_grad(func, X, h)
5% Function to compute the numerical gradient of an arbitrary objective
9% -> func: Function handle
for which numerical derivative
is to
11% -> X: Sink of interest about which derivative
is to be
13% -> h: Tolerance
for differentiation.
16% -> df: Numerical derivative of function func (vector of size
18% -> NFV: Accumulator incremented by the number of function
19% evaluations which have taken place.
21% Created by: Brendan C. Wood
22% Created on: February 14, 2011
24% Copyright (c) 2011, Brendan C. Wood <b.wood@unb.ca>
27function [df] = num_grad(func, X, h)
29df = zeros(length(X), 1);
31%
for each dimension of objective function
33 % vary variable i by a small amount (left and right)
39 % evaluate the objective function at the left and right points
43 % calculate the slope (rise/run)
for dimension i
44 df(i) = (y2 - y1) / (2*h);