Teaching Numerical Gradients and Hessians
14 Feb 2011
14 Aug 2012)
Simple, well-commented Matlab code to demonstrate how to take numerical derivatives and Hessians.
|num_grad(func, X, NFV, h)
% [df, NFV] = num_grad(func, X, NFV, h)
% Function to compute the numerical gradient of an arbitrary objective
% -> func: Function handle for which numerical derivative is to
% be obtained.
% -> X: Point of interest about which derivative is to be
% -> NFV: Accumulator to keep track of number of function
% -> h: Tolerance for differentiation.
% -> df: Numerical derivative of function func (vector of size
% -> NFV: Accumulator incremented by the number of function
% evaluations which have taken place.
% Created by: Brendan C. Wood
% Created on: February 14, 2011
% Copyright (c) 2011, Brendan C. Wood <firstname.lastname@example.org>
function [df, NFV] = num_grad(func, X, NFV, h)
df = zeros(length(X), 1);
% for each dimension of objective function
% vary variable i by a small amount (left and right)
x1 = X;
x2 = X;
x1(i) = X(i) - h;
x2(i) = X(i) + h;
% evaluate the objective function at the left and right points
[y1, NFV] = func(x1, NFV);
[y2, NFV] = func(x2, NFV);
% calculate the slope (rise/run) for dimension i
df(i) = (y2 - y1) / (2*h);