필터 지우기
필터 지우기

Calculate the gradient of a function

조회 수: 60 (최근 30일)
amine&&
amine&& 2016년 9월 13일
답변: garrett batt 2021년 8월 26일
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
John BG
John BG 2016년 9월 13일
편집: John BG 2016년 9월 13일
perhaps you would like to consider proceeding by defining x?
amine&&
amine&& 2016년 9월 14일
Thanks john, Even if i define the variable that gives me an error. Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 9월 13일
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
Walter Roberson
Walter Roberson 2016년 9월 14일
You would have to have
syms x y
first.
amine&&
amine&& 2016년 9월 14일
편집: amine&& 2016년 9월 14일
Ok Roberson. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

garrett batt
garrett batt 2021년 8월 26일

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by