How can i take the Jacobian of a function

8 次查看(过去 30 天)
Hi there, I have a problem that I need to be solved quickly. I need the Jacobian of a function with respect to one of its input values and afterwards to fill in the values of the input. These are the input values and jacobian function:
if true
x = [5 7]
b = [2 3]
h = jacobian(qn(x,b),x)
end
This is the function itself:
if true
function demand = qn(x,b)
z1=b(1)-b(2)*x(1)+b(1)*x(2);
z2=b(1)-b(2)*x(2)+b(1)*x(1);
demand = [z1;z2];
end
However, I receive an error message saying:
"Undefined function 'jacobian' for input arguments of type 'double'.
Error in Algorithm3 (line 21) h = jacobian(qn(x,b),x);"
To recap, I need the Jacobian of the function qn with respect to x(1) and x(2), so that it gives a 2 by 2 matrix back and then I want to 'input' the vectors x & b. Thanks in advance!
  2 个评论
Adam
Adam 2018-6-25
Are you expecting it to find a function called jacobian? There is one in the symbolic Toolbox, but otherwise unless you have a 3rd party one or one you have written yourself the error is understandable.
Martijn Mouw
Martijn Mouw 2018-6-25
Well I have the symbolic Toolbox and for other problems the command 'jacobian' works fine, however when I try to find the jacobian matrix of a function specified with input variables ('qn' in this case) I don't know how to tell MATLAB that I want the jacobian matrix of the output of this function with respect to x(1) and x(2)

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2018-6-25
function main
x = [5 7]
b = [2 3]
h = numerical_jacobian(@qn,x,b)
end
function df=numerical_jacobian(f,x,b)
n=length(x);
E=speye(n);
e=eps^(1/3);
for i=1:n
df(:,i)=(f(x+e*E(:,i),b)-f(x-e*E(:,i),b))/(2*e); % zentraler Differenzenquotient
end
end
function demand = qn(x,b)
z1=b(1)-b(2)*x(1)+b(1)*x(2);
z2=b(1)-b(2)*x(2)+b(1)*x(1);
demand = [z1;z2];
end
  4 个评论
Torsten
Torsten 2018-6-26
编辑:Torsten 2018-6-26
1. As far as I know, "jacobian" only works with symbolic variables.
2. Everybody uses other values as optimal h for numerical differentiation. Just test for your case which order of magnitude fits your needs.
Here is a link that derives h ~ eps^(1/3) for the central difference quotient:
https://math.stackexchange.com/questions/815113/is-there-a-general-formula-for-estimating-the-step-size-h-in-numerical-different
Best wishes
Torsten.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by