% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

% sym_hess.m
%
% Test script to compute the Hessian of an objective function both
% numerically and symbolically, in order to compare methods.
%
% Created by: Brendan C. Wood
% Created on: February 14, 2011
%
% Copyright (c) 2011, Brendan C. Wood <b.wood@unb.ca>
%

% clear workspace and command window
clear
clc

% tolerance (setting the tolerance too small may result in round-off error)
h = 1e-4;

% point of interest
X = [2, -1, 3]';

% define symbolic variables, store in symbolic array
syms('x1','x2','x3')
x_sym = sym('x_sym', [length(X), 1]);
x_sym(1) = 'x1';
x_sym(2) = 'x2';
x_sym(3) = 'x3';

% define symbolic objective function (same as in fun.m)
f = 2*x1^5 - 3*x1*x3 + 4*x2^2 - 9*x2*x3^2 + 6*x1*x2*x3;

% define symbolic array for derivatives and for Hessian
df_sym = sym('df_sym', [length(X), 1]);
H = sym('H', length(X));

% for each dimension in objective function
for i=1:length(X)
    % calculate symbolic derivative with respect to variable i
    df_sym(i) = diff(f, x_sym(i));
    
    % for each dimension in objective function
    for j=1:length(X)
        % calculate second derivative with respect to variable j
        H(i,j) = diff(df_sym(i), x_sym(j));
    end
end

% substitute numerical point of interest into symbolic matrix
H_sym = subs(H, x_sym, X)

% calculate Hessian with pure numerical method (num_hess.h)
[H_num, NFV] = num_hess(@fun, X, 0, h)
