Using symbolic gradient with another function

조회 수: 3(최근 30일)
Braden Kerr
Braden Kerr 2020년 11월 4일
댓글: Walter Roberson 2020년 11월 4일
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


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)
>> 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
Walter Roberson
Walter Roberson 2020년 11월 4일
2x1 symbolic matrix

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




Community Treasure Hunt

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

Start Hunting!

Translated by