How to feed value to jacobian() function in my case?

3 次查看(过去 30 天)
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.
  2 个评论
Jan
Jan 2016-11-17
编辑:Jan 2016-11-17
The error message tells, that the problem is in "fcn" in line 7. The code you have posted has 6 lines only. Please reveal what is in line 7. If it is the "dify" line: Is "T" a function handle?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2016-11-17
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 个评论
Lu zhang
Lu zhang 2016-11-20
This works, since I change the input array size from [1,nT] to [nT,1]. Yet another problem emerged when I tried to calculate the gradient value using MatlabFunction. Can you kindly take a look? Since the question here is already too long, I open a new question
Walter Roberson
Walter Roberson 2016-11-20
Your revised version is sending a numeric vector as variable names.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by