calculate the gradient f=@(x) (x(1)-2)^4​+(x(1)-2)^​2*x(2)^2+(​x(2)+1)^2

4 views (last 30 days)
Hello, how to calculate the gradient, Hessian matrix, of this function.Thank you
f=@(x) (x(1)-2)^4+(x(1)-2)^2*x(2)^2+(x(2)+1)^2

Accepted Answer

Star Strider
Star Strider on 11 Nov 2017
With the Symbolic Math Toolbox:
syms x1 x2
% f=@(x) (x(1)-2)^4+(x(1)-2)^2*x(2)^2+(x(2)+1)^2;
f = (x1-2)^4+(x1-2)^2*x2^2+(x2+1)^2;
Hf = hessian(f);
HF1 = matlabFunction(Hf, 'Vars', [x1 x2]);
HF2 = matlabFunction(Hf, 'Vars', {[x1 x2]});
The ‘HF1’ result returns a function of (x1,x2). The ‘HF2’ result returns a function of ‘in1’, where ‘in(:,1)=x1’ and ‘in(:,2)=x2’. Note that ‘in1’ is a row vector.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!