How can I provide the gradient of the function when using the unconstrained minimization solver fminunc
as the following options provided in the online documentation of the solver
options = optimoptions('fminunc','Algorithm','trustregion','SpecifyObjectiveGradient',true);
is not available in MATLAB 2014b.
You can use GradObj
option. For example, say we have the function x1^2+x2^4:
function [f,g]= goal(x)
f= x(1)^2+x(2)^4; % function
if nargout>1 % gradient
g= [2*x(1);4*x(2)^3];
end
end
We need to set GradObj
option:
options=optimset('GradObj','on');
Now we can obtain the solution:
x= fminunc(@goal,x0,options) % x0 is the inital point