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
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.
采纳的回答
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!