using hess(f) in matlab

The hess(f) function in Matlab is used to calculate the Hessian matrix of a function f(x), which contains the second-order partial derivatives of f with respect to each pair of variables.

Here is an example usage of hess function:

main.m
% Define the function f(x) = x1^2 + 2*x1*x2 + x2^2 + 2*x1 + 4*x2 + 3
f = @(x) x(1)^2 + 2*x(1)*x(2) + x(2)^2 + 2*x(1) + 4*x(2) + 3;

% Evaluate the Hessian matrix of f at the point [1, 2]
hess_f = hess(f, [1, 2]);
disp(hess_f);
227 chars
7 lines

This will output the following Hessian matrix:

main.m
[ 2  2 ]
[ 2  2 ]
18 chars
3 lines

Note that hess can only be used for scalar functions of several variables, and the input to hess must be a row vector of variables, not a column vector.

related categories

gistlibby LogSnag