function check_calculateerrorMJxy_derivatives
% CHECK_CALCULATEERRORMJXY_DERIVATIVES - check the gradient and Hessian for calculateerrorMJxy
% Check that the gradient and Hessian for calculateerrorMJxy have been calculated correctly
%
% This requires the functions from Adaptive Robust Numerical Differentiation, by John D'Ericco be installed
% (available for download from: http://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation )
%
% This function calculates the gradient and Hessian (first-order and
% second-order partial derivatives) using the analytic expressions in
% calculateerrorMJxy, and also using  robust adaptive numerical differentiation
% The results using the two methods are compared

load sampledata;
tangvel = sqrt(vel(:,1).^2 + vel(:,2).^2);

parameters = [0.2 0.8 -0.3 0.5];

% compute the gradient and Hessian for some sample parameters
[epsilon,grad,hess] = calculateerrorMJxy(parameters,t,vel,tangvel);

% compute using numerical differentiation
numerical_hessian = hessian(@(x) calculateerrorMJxy(x,t,vel,tangvel),parameters);
numerical_grad = gradest(@(x) calculateerrorMJxy(x,t,vel,tangvel),parameters);

numerical_grad - grad'

numerical_hessian - hess

