这是可能的,并在documentation中描述:
如果 fun 的梯度也可以计算并且 GradObj 选项为 'on',设置为
options = optimset('GradObj','on')
那么函数 fun 必须在第二个输出参数中返回梯度值 g,一个向量,在 x 处。
具有自定义渐变的 fminunc
例如:如果f = @(x) x.^2; 那么df/dx = 2*x 你可以使用
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);
带有 logx 梯度的 fminunc
对于您的 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'))
带有 logx 梯度的 fminunc 示例
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]