필터 지우기
필터 지우기

Plotting functions anf computing gradient

조회 수: 1 (최근 30일)
lateef
lateef 2023년 3월 29일
댓글: Walter Roberson 2023년 3월 29일
i am currently trying to plot a function and the compute the graident of f using the jacobian function
Plot the function f (x, y) = x sin(xy) y sin(5y).
Compute the gradient of f in using the jacobian function
my current code is shown below yet im getting erors
f = @(x,y) x*sin(x,y) - y*sin(5*y);
syms x y
jacobian([x*sin(x,y - y*sin(5*y))])
Error using sym/sin
Too many input arguments.
gradient(x*sin(x,y - y*sin(5*y)))

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 29일
sin(xy) is sin(x*y) not sin(x,y)
f = @(x,y) x*sin(x*y) - y*sin(5*y);
syms x y
jacobian(f(x,y)).'
ans = 
  댓글 수: 2
Paul
Paul 2023년 3월 29일
Is there any advantage/disadvantage to using an anonymous function vs a symfun?
syms x y
f(x,y) = x*sin(x*y) - y*sin(5*y);
jacobian(f(x,y),[x y])
ans = 
Seems to me a symfun (or just a plain sym expression) is more natural ...
Walter Roberson
Walter Roberson 2023년 3월 29일
In this particular case, I was going by the principal of least change to the original code. An anonymous function can be used with jacobian, and the user used an anonymous function, so I showed the small change to the code the user had created.
Sometimes I adjust user code to be better form, but sometimes I just show the minimal change.
More generally, the anonymous function can be used with purely numeric scalar inputs, producing double output, but a symfun would produce a symbolic output, which might or might not be acceptable to the caller.
The posted anonymous function cannot be used with non-scalar inputs. A symfun would work for this computation with nonscalars

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by