Plot function variables for each iteration in fmincon

Hi,
I am trying to plot the variables values calculated for each iteration in fmincon. @optimplotx give histogram plot but i want to plot separately each variable for my function. I am able to plot for each function evaluation, but i want the variable values to be plotted for each iteration just like @optimplotfcn.

 Réponse acceptée

Matt J
Matt J le 23 Sep 2017
Modifié(e) : Matt J le 24 Sep 2017
You can specify your own custom PlotFcn. You don't have to use one of the pre-written choices like, @optimplotx.
function stop = myplotfun(x, optimValues, state)
persistent data
if ~nargin % example reset mechanism
data=[];
end
if strcmp(state,'iter')
data=[data,x(:)];
plot(data.');
end
stop=0;
end

11 commentaires

Hi,
Thanks for your reply. I tried using the code for the plot. However i keep getting this error. I have been trying to figure out a solution but I cant find one.
Error using myplotfun.
Too many output arguments.
Error in callAllOptimPlotFcns (line 138)
state(i) = feval(plotNames{i},x,optimvalues,'init',varargin{:});
Error in barrier
Error in barrier
Error in barrier
Error in fmincon (line 798)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
I figured out the issue. i included:
function stop = myplotfun(x, optimValues, state)
stop = false; ...
Since the problem has 3 variables the x value computed is a 3Xn matrix, depending on the number of iterations. Is it possible to plot the variable values individually in subplots?
Thanks
I think myplotfun needs to return an output argument. See my revised version.
Matt J
Matt J le 24 Sep 2017
Modifié(e) : Matt J le 24 Sep 2017
Is it possible to plot the variable values individually in subplots?
Yes, it is possible. You can write any plotting code you want in myplotfun.
If this solves your issue, please click "Accept".
Yes this is perfect! thank you!
Hi Matt,
I tried plotting the variables and the fval value by defining custom function like you shared.
@myplotx
function stop = myplotx(x, optimValues, state)
persistent d1 d2 d3
if ~nargin % example reset mechanism
d1=[];
d2=[];
d3=[];
end
if strcmp(state,'iter')
d1=[d1,x(1)];
d2=[d2,x(2)];
d3=[d3,x(3)];
subplot(4,1,1)
plot(d1.','diamond');
grid on
hold on
title('Proportion Gain (Kp) Plot')
xlabel('Iterations')
ylabel('Paramater value')
legend('Gain value')
subplot(4,1,2)
plot(d2.','diamond');
grid on
hold on
title('Integral Gain (Ki) Plot')
xlabel('Iterations')
ylabel('Paramater value')
legend('Gain value')
subplot(4,1,3)
plot(d3.','diamond');
hold on
grid on
title('Derivative Gain (Kd) Plot')
xlabel('Iterations')
ylabel('Paramater value')
legend('Gain value')
end
stop = 0;
end
@myplotfval
function stop = myplotfval(fval, optimValues, state)
persistent data
if ~nargin % example reset mechanism
data=[];
end
if strcmp(state,'iter')
data=[data,fval(:)];
subplot(4,1,4)
plot(data.','diamond');
title('Function Evaluation Plot')
xlabel('Iterations')
ylabel('Function value')
legend('FVal')
grid on
end
stop = 0;
end
I think subplot is not working. Also at the end of the optimization loop i get a blank plot. What is wrong with the plot?
I would really appreciate if someone can answer this issue. When plotting all the variables together in a single plot the function works. However when i use subplots, i see the variables being plot individually, and at the end of the execution all i get it a single blank plot. I don't know what is causing this. Also the fval would not plot using subplot.
Not sure, but when I put the plot routines in an OutputFcn instead,
opts=optimoptions(@fmincon,'OutputFcn', {@myplotx,@myplotfval});
I get no problems.
Wow, i was using 'PlotFcns' instead of 'OutputFcn'. This solved the problem. Thanks Matt.
Matt J
Matt J le 25 Sep 2017
Modifié(e) : Matt J le 25 Sep 2017
PlotFcns is indeed the more natural thing to use. No idea why it doesn't work.
It seems the reset mechanism above is made wrong on purpose. For completeness, this is one that works
function stop = myplotfun(x, optimValues, state)
persistent data
if strcmp(state,'init') % example reset mechanism
data=[];
end
if strcmp(state,'iter')
data=[data,x(:)];
plot(data.');
end
stop=0;
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by