How can i take the Jacobian of a function

9 vues (au cours des 30 derniers jours)
Martijn Mouw
Martijn Mouw le 25 Juin 2018
Commenté : Martijn Mouw le 26 Juin 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 commentaires
Adam
Adam le 25 Juin 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 le 25 Juin 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)

Connectez-vous pour commenter.

Réponse acceptée

Torsten
Torsten le 25 Juin 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 commentaires
Torsten
Torsten le 26 Juin 2018
Modifié(e) : Torsten le 26 Juin 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 le 26 Juin 2018
Ah okay, I see. Thanks for the help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by