MATLAB Answers

How to define a custom plot function for an optimization

38 views (last 30 days)
Jorrit
Jorrit on 12 Aug 2011
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?

  0 Comments

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 18 Aug 2011
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 Comment

Jorrit
Jorrit on 19 Aug 2011
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by