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

6 views (last 30 days)
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 Comments
Jan
Jan on 17 Nov 2016
Edited: Jan on 17 Nov 2016
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?

Sign in to comment.

Answers (1)

Walter Roberson
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
Lu zhang on 20 Nov 2016
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 on 20 Nov 2016
Your revised version is sending a numeric vector as variable names.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!