LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
num_hess.m
1% num_hess.m
2%
3% [H, NFV] = num_hess(func, X, NFV, h)
4%
5% Function to compute the numerical Hessian of an arbitrary objective
6% function.
7%
8% Inputs:
9% -> func: Function handle for which numerical Hessian is to be
10% obtained.
11% -> X: Sink of interest about which Hessian is to be obtained.
12% -> NFV: Accumulator to keep track of number of function
13% evaluations.
14% -> h: Tolerance for differentiation.
15%
16% Outputs:
17% -> H: Numerical Hessian of function func (square symmetric
18% matrix of size n=length(X)).
19% -> NFV: Accumulator incremented by the number of function
20% evaluations which have taken place.
21%
22% Created by: Brendan C. Wood
23% Created on: February 14, 2011
24%
25% Copyright (c) 2011, Brendan C. Wood <b.wood@unb.ca>
26%
27
28function [H] = num_hess(func, X, h)
29
30H = zeros(length(X));
31
32% for each dimension of objective function
33for i=1:length(X)
34 % derivative at first point (left)
35 x1 = X;
36 x1(i) = X(i) - h;
37 [df1] = num_grad(func, x1, h);
38
39 % derivative at second point (right)
40 x2 = X;
41 x2(i) = X(i) + h;
42 [df2] = num_grad(func, x2, h);
43
44 % differentiate between the two derivatives
45 d2f = (df2-df1) / (2*h);
46
47 % assign as row i of Hessian
48 H(i,:) = d2f';
49end