Home > . > hessian.m

hessian

PURPOSE ^

Copyright (C) 2001 Michel Juillard

SYNOPSIS ^

function hessian_mat = hessian(func,x,varargin)

DESCRIPTION ^

 Copyright (C) 2001 Michel Juillard
% computes second order partial derivatives
 uses Abramowitz and Stegun (1965) formulas 25.3.24 and 25.3.27 p. 884

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % Copyright (C) 2001 Michel Juillard
0002 %% computes second order partial derivatives
0003 % uses Abramowitz and Stegun (1965) formulas 25.3.24 and 25.3.27 p. 884
0004 
0005 function hessian_mat = hessian(func,x,varargin)
0006   global options_
0007   func = str2func(func);
0008   n=size(x,1);
0009   %h1=max(abs(x),options_.gstep*ones(n,1))*eps^(1/3);
0010   h1=max(abs(x),sqrt(options_.gstep)*ones(n,1))*eps^(1/6);
0011   h_1=h1;
0012   xh1=x+h1;
0013   h1=xh1-x;
0014   xh1=x-h_1;
0015   h_1=x-xh1;
0016   xh1=x;
0017   f0=feval(func,x,varargin{:});
0018   f1=zeros(size(f0,1),n);
0019   f_1=f1;
0020   for i=1:n    
0021     xh1(i)=x(i)+h1(i);
0022     f1(:,i)=feval(func,xh1,varargin{:});
0023     xh1(i)=x(i)-h_1(i);
0024     f_1(:,i)=feval(func,xh1,varargin{:});
0025     xh1(i)=x(i);
0026     i=i+1;
0027   end
0028   xh_1=xh1;
0029   hessian_mat = zeros(size(f0,1),n*n);
0030   for i=1:n    
0031     if i > 1        
0032       k=[i:n:n*(i-1)];
0033       hessian_mat(:,(i-1)*n+1:(i-1)*n+i-1)=hessian_mat(:,k);
0034     end     
0035     hessian_mat(:,(i-1)*n+i)=(f1(:,i)+f_1(:,i)-2*f0)./(h1(i)*h_1(i));
0036     temp=f1+f_1-f0*ones(1,n);
0037     for j=i+1:n        
0038       xh1(i)=x(i)+h1(i);
0039       xh1(j)=x(j)+h_1(j);
0040       xh_1(i)=x(i)-h1(i);
0041       xh_1(j)=x(j)-h_1(j);
0042       hessian_mat(:,(i-1)*n+j)=-(-feval(func,xh1,varargin{:})-feval(func,xh_1,varargin{:})+temp(:,i)+temp(:,j))./(2*h1(i)*h_1(j));
0043       xh1(i)=x(i);
0044       xh1(j)=x(j);
0045       xh_1(i)=x(i);
0046       xh_1(j)=x(j);
0047       j=j+1;
0048     end    
0049     i=i+1;
0050   end 
0051   % 11/25/03 SA Created from Hessian_sparse (removed sparse)

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003