This is possible and is described in the documentation :
If the fun gradient can also be calculated, and the GradObj 'on' option, as set by options = optimset ('GradObj', 'on'), then the fun function should return the gradient value g, vector, in x in the second output argument.
fminunc with custom gradient
So for example: if f = @(x) x.^2;then df/dx = 2*x, and you can use
function [f df] = f_and_df(x)
f = x.^2;
if nargout>1
df = 2*x;
end
end
fminunc:
options = optimset('GradObj','on');
x0 = 5;
[x,fval] = fminunc(@f_and_df,x0,options);
fminunc logx
logx :
function [f df] = f_and_df(x)
f = ...;
if nargout>1
df = x * (dF/logx);
end
end
fminunc .
fminunc
, :
f_and_df2 = @(x) deal(x(1).^2+x(2).^2,[2*x(1) 2*x(2)]);
[x,fval] = fminunc(f_and_df2,[5, 4],optimset('GradObj','on'))
fminunc logx
f = (log(x))^2
function [f df_dlogx] = f_and_df(x)
f = log(x).^2;
df_dx = 2*log(x)./x;
df_dlogx = df_dx.* x;
end
:
>>x0=3;
>>[x,fval] = fminunc(@f_and_df,x0,optimset('GradObj','on'))
x =
0.999999990550151
fval =
8.92996430424197e-17
fminunc
, . f (x, y), , :
function [f df_dx] = f_and_df(x)
f = x(1).2 + x(2).^2;
df_dx(1) = 2*x(1);
df_dx(2) = 2*x(2);
end
.
, , : x0 = [- 5 3]