I've got an error message: Undefined variable x when I call up function Hessian in Matlab
Show older comments
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
Geoff Hayes
on 4 Dec 2015
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.
Hmissi bochra
on 4 Dec 2015
Edited: Geoff Hayes
on 4 Dec 2015
Torsten
on 4 Dec 2015
You don't write how you invoke dcc_hessian.
Best wishes
Torsten.
Hmissi bochra
on 4 Dec 2015
Walter Roberson
on 4 Dec 2015
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.)
Hmissi bochra
on 4 Dec 2015
Answers (1)
Geoff Hayes
on 4 Dec 2015
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.
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!