Symbolic Hessian Evaluator



01 Dec 2007 (Updated )

For a symbolic input function f, returns the symbolic Hessian matrix. A real time saver imho -_-

function H = hessian(f,varlist)
%Finds the Hessian matrix symbolically for symbolic input function f.
%To use it, type syms <variables>.
%for example:
%syms x y
%f = 3*x*y^2
%varlist is the list of variables in f.
%hessian(f,[x y])
%ans =
%[   0, 6*y]
%[ 6*y, 6*x]
%(c) Brad Ridder 2007. Feel free to use this under the GPL guidelines. If
%you wish to add to this program, just leave my name and add yours to it.
n = numel(varlist);
hess = vpa(ones(n,n));
for j = 1:n;
    for i = 1:n;
        hess(j,i) = diff(diff(f,varlist(i),1),varlist(j),1);

Contact us