Clear Filters
Clear Filters

Using symbolic gradient with another function

2 views (last 30 days)
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

Answers (1)

Ameer Hamza
Ameer Hamza on 4 Nov 2020
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

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!