creating a function getting the graident and passing variables to them

1 回表示 (過去 30 日間)
Nora Khaled
Nora Khaled 2019 年 3 月 11 日
コメント済み: Nora Khaled 2019 年 3 月 11 日
Hi !
I am trying to creat a function that I can both use to pass values and compute the gradient of that function.
I tried both these :
but the for the first I cant pass values and the 2nd the gradient function in matlab does not work
syms x1 x2
f= 100*(x2-x1^2)^2 + (1+x1)^2;
gr= gradient(f)
%%%%%% other way
syms x1 x2
f=@(x1,x2) 100*(x2-x1^2)^2 + (1+x1)^2;
gr= gradient(f)
  2 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 3 月 11 日
Nora - please clarify what you mean by but the for the first I cant pass values and the 2nd the gradient function in matlab does not work. What do you mean by this? Are you observing errors? If so, what are they?
Nora Khaled
Nora Khaled 2019 年 3 月 11 日
yes there were some error when I try to do f(2,3) for example and the second one had error related to the gradient function not accepting a function was created using f=@(x,y).
but it currntly works using what was suggested in the answers.
thank you !

サインインしてコメントする。

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 3 月 11 日
Is this that you want?
syms f(x1,x2)
f(x1,x2)= 100*(x2-x1^2)^2 + (1+x1)^2;
gr(x1,x2) = gradient(f,[x1,x2])
Then you can compute
f(0,0)
gr(0,0)

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by