I've got an error message: Undefined variable x when I call up function Hessian in Matlab

I'm trying to write an implementation of a Hessian function in Matlab.
When I call up my function using the formula:
n = size(x,1);
fx = feval(f,x,varargin{:});
% Compute the stepsize (h)
h = eps.^(1/3)*max(abs(x),1);
xh = x+h;
h = xh-x;
ee = sparse(1:n,1:n,h,n,n);
% Compute forward step
g = zeros(n,1);
for i=1:n
g(i) = feval(f,x+ee(:,i),varargin{:});
end
H=h*h';
% Compute "double" forward step
index=1;
for i=1:n
for j=(n-N+1):n
% fprintf('Evaluating Function %d out of %d\n',index,n*N);
if i<=j;
H(i,j) = (feval(f,x+ee(:,i)+ee(:,j),varargin{:})-g(i)-g(j)+fx)/H(i,j);
H(j,i) = H(i,j);
end
index=index+1;
end
end
newH=H((n-N+1):n,:);
H=newH;
I've got an error message:
Undefined variable x
I have no idea what x is. Maybe someone more familiar with Matlab can help me.

6 Comments

Hmissi - your first line of code is
n = size(x,1);
Is this the line that is generating the error? If so, then the error is telling you that the x has not yet been defined prior to it. Is the above code part of a script or a function, and if the latter, what is the function signature? Have you written this code or obtained it from somewhere else?
Presumably (at least) x and f are inputs into this function. Please post all of the code and how you are invoking it.
function H = dcc_hessian(f,x,N,varargin)
n = size(x,1);
fx = feval(f,x,varargin{:});
% Compute the stepsize (h)
h = eps.^(1/3)*max(abs(x),1);
xh = x+h;
h = xh-x;
ee = sparse(1:n,1:n,h,n,n);
% Compute forward step
g = zeros(n,1);
for i=1:n
g(i) = feval(f,x+ee(:,i),varargin{:});
end
H=h*h';
% Compute "double" forward step
index=1;
for i=1:n
for j=(n-N+1):n
% fprintf('Evaluating Function %d out of %d\n',index,n*N);
if i<=j;
H(i,j) = (feval(f,x+ee(:,i)+ee(:,j),varargin{:})-g(i)-g(j)+fx)/H(i,j);
H(j,i) = H(i,j);
end
index=index+1;
end
end
newH=H((n-N+1):n,:);
H=newH;
You don't write how you invoke dcc_hessian.
Best wishes
Torsten.
You are passing varargin in a function call?? Unusual, but not necessarily invalid.
I notice you are not calling dcc_hessian though. Or is H a function handle that has been assigned @dcc_hessian ?
(If the above is not the actual line of code that you use to invoke dcc_hessian, please show the actual line.)
I Don't Know How To invoke This function in command window ? please someone can help me!!

Sign in to comment.

Answers (1)

Hmissi - from the command line, you would call this function as (at the very least)
H = dcc_hessian(f,x,N)
where f, x, and N are "something" that you have already defined. So what should these be? Presumably you have copied this code from somewhere on the web, but have removed the comments that indicate what the input and output parameters are, and, more importantly, the author of the code
% PURPOSE:
% Special purpose hessian for use with dcc std errors
%
% USAGE:
% H = dcc_hessian(func,x,N,varargin)
%
%
% INPUTS:
% f = function name, feval = func(x,varargin)
% x = vector of parameters (n x 1)
% N = the last N rows to be completed
% varargin = optional arguments passed to the function
%
% OUTPUTS:
% H = the N last rows of the hessian
% used to speed up dcc_garch
%
% COMMENTS:
%
%
% Author: Kevin Sheppard
% kevin.sheppard@economics.ox.ac.uk
% Revision: 2 Date: 12/31/2001
So once you have figured out your function f, your vector of parameters x, and the last N rows to be completed, you should be set to call this function.
If you are still stuck, you may wish to contact the author of the code.

Asked:

on 3 Dec 2015

Answered:

on 4 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!