Finding the gradient and the hessian of the same function

If I have the function
fv = @(x) x(1)^2 + x(1)*x(2) + (3/2)*x(2)^2 - 2*log(x(1)) - log(x(2));
What would be the best way to find the hessian of said function?

 Accepted Answer

Read about gradient, hessian.

7 Comments

When I try computing the hessian, I get the following
fv = @(x) x(1)^2 + x(1)*x(2) + (3/2)*x(2)^2 - 2*log(x(1)) - log(x(2));
hessian(fv,[x(1),x(2)])
Unrecognized function or variable 'x'.
jacobian(gradient(fv))
syms x y
fv = x^2 + x*y + (3/2)*y^2 - 2*log(x) - log(y);
hessian(fv,[x,y])
If you want x(1) and x(2) like:
syms x [1 2]
fv = x(1)^2 + x(1)*x(2) + (3/2)*x(2)^2 - 2*log(x(1)) - log(x(2));
hessian(fv,x)
And how would we find the gradient?
In the same way...read the doc.
syms x [1 2]
fv = x(1)^2 + x(1)*x(2) + (3/2)*x(2)^2 - 2*log(x(1)) - log(x(2));
%hessian(fv,x)
[x(1),x(2)] = gradient(fv)
Error using sym/gradient
Too many output arguments.
I get an error when trying to use gradient function
Same way means:
syms x [1 2]
fv = x(1)^2 + x(1)*x(2) + (3/2)*x(2)^2 - 2*log(x(1)) - log(x(2));
g = gradient(fv,x)
g = 

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Asked:

on 7 Nov 2022

Commented:

on 7 Nov 2022

Community Treasure Hunt

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

Start Hunting!