How to feed value to jacobian() function in my case?
Show older comments
I need to calculate the gradient of a user-defined object function named fcn. I choose to use jacobian() function to calculate the symbolic gradient.
take subset of parameter vector b as input, like
%******************************
% Parameterized Moments
%******************************
function fm = fcn(b)
global T T_z T_e zt et
teta =0;
zt =b(1:T_z); % Line-7 %
et =b(1+T_z:T_z+T_e);
dify =zeros(T,T);
b are pre-calculated parameters. so I write the jacobian() in the following way to call fcn
v = [zt,et]';
dfb = jacobian(fcn,v);
but Matlab told me that
Error using fcn (line 7)
Not enough input arguments.
I'm not sure if this is due to the way I write jacobian or how I define the fcn? How can I fix the error? Thank you.
Answers (1)
Walter Roberson
on 17 Nov 2016
When you call
jacobian(fcn,v)
then v must evaluate to a vector of symbolic variable names, and fcn must evaluate to a symbolic expression or symbolic function.
When fcn is a function, using it in a syntax such as
jacobian(fcn,v)
causes the function to be invoked with no arguments, exactly equivalent to
jacobian(fcn(),v)
Your fcn function does not like being called with no arguments.
To calculate the jacobian with that fcn, it appears to me you would need to use something like
global T_z T_e zt et
nT = T_z + T_e;
B = sym('B', [1, nT]);
temp = fcn(B); %zt and et are not defined until fcn is run
v = [zt,et]';
dfb = jacobian(temp, v);
10 Comments
Lu zhang
on 18 Nov 2016
Walter Roberson
on 18 Nov 2016
You currently have
dify =zeros(T,T);
Immediately after that, add
if isa(b, 'sym')
dify = sym(dify);
end
Lu zhang
on 18 Nov 2016
Walter Roberson
on 18 Nov 2016
You went back to
dfb = jacobian(fcn,b);
That can only work if fcn is a symbolic function or symbolic expression, not if fcn is a function handle. You need the code like I showed
nT = T_z + T_e;
B = sym('B', [1, nT]);
temp = fcn(B); %zt and et are not defined until fcn is run
v = [zt,et]';
dfb = jacobian(temp, v);
Walter Roberson
on 19 Nov 2016
When you make the change from fcn to temp did you also change the b to v? You have
dfb = jacobian(fcn,b);
but your b is not a vector of variable names, it is numeric.
Walter Roberson
on 19 Nov 2016
Odd, it worked when I tested before. I will test with your version later today
Lu zhang
on 20 Nov 2016
Walter Roberson
on 20 Nov 2016
Your revised version is sending a numeric vector as variable names.
Categories
Find more on Conversion 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!