How to define a custom plot function for an optimization

조회 수: 9 (최근 30일)
Jorrit
Jorrit 2011년 8월 12일
Hi,
I want to define a custom plotfunction for my optimization. It should be a bit like the standard 'optimplotfval' plot function, but with a few extras. The information available is a bit unclear to me. It says that I have to define it just like the OutFnc, which I'm using too. If I'm using the same structure, I get an error message:
??? Error using ==> opt>objectiveconstraints/plotfun
Too many output arguments.
Error in ==> callAllOptimPlotFcns at 62
state(i) = feval(plotNames{i},x,optimvalues,'init',varargin{:});
Error in ==> nlconst>callOutputAndPlotFcns at 1032
stop = callAllOptimPlotFcns(plotfcns,xOrigShape,optimValues,state,varargin{:}) || stop;
Error in ==> nlconst at 559
[xOrigShape, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,caller,problemInfo, ...
Error in ==> fmincon at 724
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in ==> opt>objectiveconstraints at 260
[x,fval] = fmincon(problem);
Error in ==> opt at 50
[x,fval,aircraft] = objectiveconstraints(aircraft,problem);
I have defined my plot function in the same way as the OutFnc:
%Define function handles
objective=@(x)obj(aircraftstart,x);
constraint=@con;
outputfunction=@outfun;
plotfunction=@plotfun;
The function is nested inside the main function and looks like this:
function [] = plotfun(x,optimValues,state)
figure(20)
hold on
if optimValues.constrviolation<=1E-6
plot(optimValues.iteration,optimValues.fval,'g')
else
plot(optimValues.iteration,optimValues.fval,'r')
end
hold off
end
I don't have any output from the plot function, yet it still says I have too many output arguments. The options of fmincon are:
problem.options = optimset('Display','iter','MaxIter',1000,'OutputFcn',outputfunction,'FunValCheck','on','Algorithm','active-set','DiffMinChange',0.05,'DiffMaxChange',10.0,'PlotFcns',plotfunction,'TolX',1e-20);
Any thoughts?

채택된 답변

Alan Weiss
Alan Weiss 2011년 8월 18일
Your line
function [] = plotfun(x,optimValues,state)
should be
function stop = plotfun(x,optimValues,state)
Inside your function you should have a line
stop = false;
By the way, you should not set TolX to anything less than a few eps, such as 4*eps. I would not set it to less than 1e-14 without a very good reason. You have such a large value of DiffMinChange that I think you should not set TolX at all, or perhaps set it to DiffMinChange/10.
  댓글 수: 1
Jorrit
Jorrit 2011년 8월 19일
Thanks!
About the TolX, I have removed that now, doesn't seem like it's making a difference. I'm still struggling with the optimization setup and it was still there, because I just tried a lot of stuff.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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