Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Set optimset('OutputFcn',@optimplotfval)

540 views
Skip to first unread message

Armindo

unread,
Apr 20, 2011, 6:39:04 AM4/20/11
to
Hi
Iam trying to use multistart (I think Iam almost there) but for that Iam creating the following problem and used the lsqcurvefit :

problem = createOptimProblem('lsqcurvefit',...
'objective',model, 'xdata', t, 'ydata', F,'x0',x0,...
'lb', [0 0 -3000 -4000 0 -5],...
'ub', [10 50 3000 5000 800 5],...
'options',optimset('OutputFcn',@PlotIterates))

I want to see a plot every iteration showing the curve fitt obtained. Like in this exemple:
http://www.mathworks.com/products/demos/global-optimization/multistartWebDemo/

However I was not able to do this:

The PlotIterates function is something like this:

stop = PlotIterates (x, optimValues, state)

and the function F that I want to plot is this

F= exp(-x(1).*xdata).*(x(3).*cos(x(2).*xdata)+x(4).*sin(x(2).*xdata))+ x(6).*xdata + x(5);

plot(xdata,F,'b')

How can I get xdata to be able to plot? Please help...

Armindo

unread,
Apr 20, 2011, 5:58:05 PM4/20/11
to
please help....

Steve

unread,
Apr 21, 2011, 10:07:04 AM4/21/11
to
"Armindo" wrote in message <iomd48$d1e$1...@fred.mathworks.com>...

You need to pass the xdata into the PlotIterates function. Change the definition of PlotIterates to look like this:

function stop = PlotIterates (x, optimValues, state, xdata)
...

Then change the call to this (an anonymous function):

myOutputFcn = @(x,optimValues,state) PlotIterates (x, optimValues, state, xdata)


problem = createOptimProblem('lsqcurvefit',...
'objective',model, 'xdata', t, 'ydata', F,'x0',x0,...
'lb', [0 0 -3000 -4000 0 -5],...
'ub', [10 50 3000 5000 800 5],...

'options',optimset('OutputFcn',myOutputFcn))

Armindo

unread,
Apr 21, 2011, 4:30:22 PM4/21/11
to
Hi Steve thank you for the help. But is not working.

I get this error


??? Error using ==> createOptimProblem at 92
Arguments must occur in name-value pairs.

Armindo

unread,
Apr 21, 2011, 4:41:05 PM4/21/11
to
The error was corrected. It was missing the word 'options'. However Iam not able so see in the same graph the data changing in each iteraction. I just can see um graph at the end with many superimposing lines

Steve Grikschat

unread,
Apr 22, 2011, 10:38:05 AM4/22/11
to
"Armindo" wrote in message <ioq4p1$rpv$1...@fred.mathworks.com>...

> The error was corrected. It was missing the word 'options'. However Iam not able so see in the same graph the data changing in each iteraction. I just can see um graph at the end with many superimposing lines

You'll have to check out your PlotFcn or OutputFcn to be sure that it works.

SAGAR

unread,
Jan 23, 2014, 9:29:07 AM1/23/14
to
Hi Armindo

i am also facing the same problem, while using the 'fmincon' for my optimization problem. could you please help me getting rid of it...?
though i have put the functions in the suggested way [told here, as well as in MATLAB help]; still the output function is shown at the end only... where i intend to see the plot after each iteration

thanks
sagar

Alan_Weiss

unread,
Jan 23, 2014, 10:34:35 AM1/23/14
to
Perhaps you need to add a drawnow command.

Alan Weiss
MATLAB mathematical toolbox documentation

SAGAR

unread,
Jan 24, 2014, 4:16:07 AM1/24/14
to
thank you very much Alan,
it worked now, one more thing i chnaged- now instead of 'OutputFun', i used 'PlotFcns'.
though the two appear to perform similar functions, as shown in MATLAB help; yet they are acting differently

Vaishnovi Sekar

unread,
Jan 28, 2017, 11:18:08 PM1/28/17
to
Hey Alan,
I am working with the saoptimset and PlotFcns for saplotf to get the function value. This ends in a series of errors. When I try to use PlotFcn and @optimplotfval, this seems to work fine with out any error and I also get to see the current function value change as the title. But there is no graph plotted. I read your suggestion to add drawnow command. I tried to edit the optimplotfval and add drawnow command as suggested in some of the examples. This doesnt work. May be I am not doing the correct way. I am too basic in matlab and trying to work on an assignment with built in simulated annealing options. Kindly suggest me the write way to do this. Many thanks!

Alan_Weiss <awe...@mathworks.com> wrote in message <lbrcqb$i1v$1...@newscl01ah.mathworks.com>...

Alan Weiss

unread,
Jan 30, 2017, 11:12:44 AM1/30/17
to
I'm sorry, but I don't understand what you are asking. Do you mean to
say that @saplotf does not draw anything when you include it as a plot
function? Or did you mean that it works fine, but your custom plot
function does not work? When I run the following code, I see a plot as
the optimization progresses:

fun = @dejong5fcn;
options = optimoptions(@simulannealbnd,'PlotFcn',@saplotf)
lb = [-50,-50];
ub = -lb;
x = simulannealbnd(fun,lb,lb,ub,options)

As for a custom plot function, the documentation has a description of
the required syntax:
https://www.mathworks.com/help/gads/simulated-annealing-options.html#bq26j8s-2

The genetic algorithm plot function syntax is different, but maybe an
example of that would help anyway:
https://www.mathworks.com/help/gads/creating-a-custom-plot-function.html

Good luck,

Vaishnovi Sekar

unread,
Jan 30, 2017, 4:56:11 PM1/30/17
to
Hey Alan,
Thanks for your reply. What I mean is @saplotf does not draw anything when you include it as a plot function.
Case 1: This is what I am actually trying to do.
So I have this as my codes,
...
options = saoptimset('MaxFunEvals', 10000, 'InitialTemperature', 10000, 'PlotFcns',{@saplotx,@saplotf});
...
...
[x,cost_x]=simulannealbnd(@COST_function,x0,lb,ub, options);

I have my lower bound and upper bound defined. I did not do change anything in them, I do not get the plot when I try to construct them.

Case 2: I tried your example in my codes. So i changed the following.
...
options = optimoptions(@simulannealbnd,'PlotFcn',@saplotf);
...
The rest of my code is the same.
And I have a series of error stating "Invalid solver specified. Provide a solver name
or handle (such as 'fmincon' or @fminunc).
Type DOC OPTIMOPTIONS for a list of solvers.

Error in simulbnd (line 36)
options =
optimoptions(@simulannealbnd,'PlotFcn',@saplotf);"

I am confused. Kindly suggest. Many thanks!
Vaishnovi

Alan Weiss <awe...@mathworks.com> wrote in message <o6noln$kov$1...@newscl01ah.mathworks.com>...

Vaishnovi Sekar

unread,
Jan 30, 2017, 4:57:08 PM1/30/17
to
Hey Alan,
Thanks for your reply. What I mean is @saplotf does not draw anything when you include it as a plot function.
Case 1: This is what I am actually trying to do.
So I have this as my codes,
...
options = saoptimset('MaxFunEvals', 10000, 'InitialTemperature', 10000, 'PlotFcns',{@saplotx,@saplotf});
...
...
[x,cost_x]=simulannealbnd(@COST_function,x0,lb,ub, options);

I have my lower bound and upper bound defined. I did not do change anything in them, I do not get the plot when I try to construct them.

Case 2: I tried your example in my codes. So i changed the following.
...
options = optimoptions(@simulannealbnd,'PlotFcn',@saplotf);
...
The rest of my code is the same.
And I have a series of error stating "Invalid solver specified. Provide a solver name
or handle (such as 'fmincon' or @fminunc).
Type DOC OPTIMOPTIONS for a list of solvers.

Error in simulbnd (line 36)
options =
optimoptions(@simulannealbnd,'PlotFcn',@saplotf);"

I am confused. Kindly suggest. Many thanks!
Vaishnovi

Alan Weiss <awe...@mathworks.com> wrote in message <o6noln$kov$1...@newscl01ah.mathworks.com>...
>

Alan Weiss

unread,
Jan 31, 2017, 8:03:18 AM1/31/17
to
Well, you must have an older MATLAB version, from before the time that
simulannealbnd was supported in optimoptions. So you would replace the
optimoptions call with

options = saoptimset('PlotFcns',@saplotf)

As a test if this does not produce a plot for you, try entering

which -all saplotf

You may have an invalid saplotf.m file somewhere that is messing up your
plotting. If not, you might need to reinstall your Global Optimization
Toolbox files.

Vaishnovi Sekar

unread,
Feb 3, 2017, 6:16:07 AM2/3/17
to
Hi Alan,
I did check my saplotf file. It doesnt seem to be messed up. However When I run my simulbnd matlab script with this option it results in series of error stating valid VectorData value. Complex inputs are not supported, error with values of YData and Xdata and several error in lines in the built in function files.
Now this is what I tried instead. I changed my option construct to this,

options = saoptimset('MaxFunEvals', 10000, 'InitialTemperature', 10000, 'PlotFcn',@optimplotfval);

I get NO error, the program runs, completes its entire Evaluations. and the Graph or figure window is displayed. I get the change in the value of the function. I get this current function value in the title bar as it progresses. BUT, I dont get a graph plot with it. I checked the optimplotfval. It doesnt seem messed up anywhere as well. I was wondering if I should add a drawnow command. And I tried a couple of combinations too. Did really change anything that I was getting previously, a blank figure window with the change of current functional value as string in the title not as a plot. What should I do to make it draw the plot. Please suggest.

Alan Weiss <awe...@mathworks.com> wrote in message <o6q1ui$rqq$1...@newscl01ah.mathworks.com>...

Alan Weiss

unread,
Feb 3, 2017, 10:39:34 AM2/3/17
to
I'm sorry, but I didn't notice something before. You say that you are using

'PlotFcn',@optimplotfval

This plot function does not work with simulannealbnd. It is not intended
to. Instead, as I thought you were doing, you should use
'PlotFcns',@saplotf. That is the correct plot function for simulannealbnd.

Vaishnovi Sekar

unread,
Feb 3, 2017, 11:13:07 AM2/3/17
to
Alan Weiss <awe...@mathworks.com> wrote in message <o7287i$ld9$1...@newscl01ah.mathworks.com>...
>
> I'm sorry, but I didn't notice something before. You say that you are using
>
> 'PlotFcn',@optimplotfval
>
> This plot function does not work with simulannealbnd. It is not intended
> to. Instead, as I thought you were doing, you should use
> 'PlotFcns',@saplotf. That is the correct plot function for simulannealbnd.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Dear Alan, I know its not the right fitting but it works. It works and 'PlotFcns',@saplotf. does not. And I checked about all my updates, as you suggested. Everything is up-to-date and also correctly installed. I dont really know how to fix this "Complex variable value issue with the YData" error in 'PlotFcns',@saplotf.. Suggestions?

Alan Weiss

unread,
Feb 3, 2017, 12:45:47 PM2/3/17
to
I'm sorry, but I don't think that you ever said what errors you see. Can
you report specifically what happens?

In particular, if your objective function returns complex values, then
you should first correct your objective function so that it does not
produce complex values.
0 new messages