Provide gradient for Fmincon

조회 수: 59 (최근 30일)
Dat Tran
Dat Tran 2016년 2월 16일
답변: Walter Roberson 2016년 2월 16일
Dear all,
How can I include Gradient for Objective Function in code below?
Thank you so much for your help!
Dat
function [p,fval] = MC_NT(p0,Aeq,beq,N)
if nargin < 5
opts = optimoptions('fmincon','Algorithm','interior-point');
end
M=length(p0);
pr1=[0.3185 0.0001 0.1574 0.2902 0.0003 0.00001 0.8426 0.7098 0.6804 0.0822 0.00001 0.00001 0.0008 0.9177 0.00001 0.00001];
pr=horzcat(pr1,(0.25*ones(48,1)'))';
p=nan(M,N);
fval=nan(1,N);
for i=1:N
fun=@(p) sum(p.*log(p./pr));
[p(:,i),fval(i)] = fmincon(fun,p0,[],[],Aeq,beq,[],[],[],opts);
pr=p(:,i);
end

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 16일
You would have to recode fun as a real function instead of anonymous function, as it is difficult for an anonymous function to return multiple outputs.
The function you provide would need to return the gradient in the second output.
You would need to specify the 'GradObj', 'on' option in your optimoptions()
There is an example showing a gradient calculation at http://www.mathworks.com/help/optim/ug/fmincon.html#busxd7j-1
As you have linear equality constraints but no bounds constraints and no non-linear constraints, you would have the option of switching to trust-region-reflective algorithm once you provide the gradient.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by