Calculate the gradient of a function

49 vues (au cours des 30 derniers jours)
amine&&
amine&& le 13 Sep 2016
Hello. I want to calculate the gradient of the function :
g={@(x)-x;@(x)x-1;@(y)-y;@(y)y-1};
with the following command :
gradient(g, [x, y]);
but I get the following error :
Undefined function or variable 'x'.
How I should proceed? Thanks.
  2 commentaires
John BG
John BG le 13 Sep 2016
Modifié(e) : John BG le 13 Sep 2016
perhaps you would like to consider proceeding by defining x?
amine&&
amine&& le 14 Sep 2016
Thanks john, Even if i define the variable that gives me an error. Thanks.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Sep 2016
You are confusing the numeric gradient function http://www.mathworks.com/help/matlab/ref/gradient.html with the symbolic gradient function, http://www.mathworks.com/help/symbolic/gradient.html
On the other hand, neither gradient() accepts a vector or cell array of function handles. Numeric gradient() accepts a numeric vector or array, and spacing distances for each of the dimensions. Symbolic gradient() accepts a scalar symbolic expression or symbolic function together with the variables to take the gradient over.
When you have a vector of functions to work with, you almost certainly want a jacobian rather than a gradient. Symbolic jacobian http://www.mathworks.com/help/symbolic/jacobian.html accepts a symbolic expression, symbolic function, or symbolic vector (but not a cell array of function handles.)
  3 commentaires
Walter Roberson
Walter Roberson le 14 Sep 2016
You would have to have
syms x y
first.
amine&&
amine&& le 14 Sep 2016
Modifié(e) : amine&& le 14 Sep 2016
Ok Roberson. Thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

garrett batt
garrett batt le 26 Août 2021

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by