Using symbolic gradient with another function

조회 수: 5 (최근 30일)
Braden Kerr
Braden Kerr 2020년 11월 4일
댓글: Walter Roberson 2020년 11월 4일
Hello,
I have a question regarding the use of symbolic gradient.
I have an initial function, shown below, that is a function of two variables, x(1) and x(2).
fun = @(x) -9*x(1) -10*x(2) + theta*(-log(100-x(1)-x(2))-log(x(1))-log(x(2)) - log(50-x(1)+x(2)));
This function takes in x, which is a 2x1 matrix. What I am hoping to do is taking the symbolic gradient of fun and get the output as another 2x1 matrix with the associated equations. Is this possible?
I would like to get a vector that is the following
[%gradient(fun) for x(1); %gradient of fun for x(2)]
I am currently attempting to use the line of code:
grad_fun = @(x) gradient(x,[x(1),x(2)]);
But it returns an error
I will clarify anthing necessary.
Thank you

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 4일
Symbolic gradient require that you define it as a symbolic expression
syms x [1 2]
syms theta
fun = -9*x(1) -10*x(2) + theta*(-log(100-x(1)-x(2))-log(x(1))-log(x(2)) - log(50-x(1)+x(2)));
d_fun = gradient(fun, x)
Result
>> d_fun
d_fun =
- theta*(1/(x1 + x2 - 100) - 1/(x2 - x1 + 50) + 1/x1) - 9
- theta*(1/(x2 - x1 + 50) + 1/(x1 + x2 - 100) + 1/x2) - 10
  댓글 수: 2
Braden Kerr
Braden Kerr 2020년 11월 4일
Is your result, d_fun, a 2x1 matrix or just an output?
Walter Roberson
Walter Roberson 2020년 11월 4일
2x1 symbolic matrix

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

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by