How can i take the Jacobian of a function

8 Ansichten (letzte 30 Tage)
Martijn Mouw
Martijn Mouw am 25 Jun. 2018
Kommentiert: Martijn Mouw am 26 Jun. 2018
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 Kommentare
Adam
Adam am 25 Jun. 2018
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 am 25 Jun. 2018
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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 25 Jun. 2018
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 Kommentare
Torsten
Torsten am 26 Jun. 2018
Bearbeitet: Torsten am 26 Jun. 2018
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.
Martijn Mouw
Martijn Mouw am 26 Jun. 2018
Ah okay, I see. Thanks for the help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by