using fminsearch--how to realize the display of the x values in every step of iteration?Thank you!!!

30 views (last 30 days)
We all know the Display option in the fminsearch function can show the procedure during the iterations.But there are only 4 columns of datas--Iteration , Func-count , min f(x) , Procedure. And I want to know how to make the current x values (the variable values) of the current iteration?
like this:
x =
0.125007539673239 -0.532250280724846
rather than displaying the final result only.
But,how? Thanks a lot!
For saving your time,you can explain your methods with the example below,thank you!
The outfun function:
outfun.m:
function stop = outfun( x,optimValues,state )
stop=false;
hold on;
plot(x(1),x(2),'*');
drawnow
end
the script which calls the outfun function: OutputFcn1.m:
options=optimset('OutputFcn',@outfun,'Display','iter','MaxIter',30)
hold on
objfun=@(x)exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2))
[x,fval,exitflag,output]=fminsearch(objfun,[-1,1],options)
hold off

Answers (1)

Steven Lord
Steven Lord on 9 May 2018
See the examples in this page in the documentation.
You can write your OutputFcn to do whatever output logging or display you want. The first example on that documentation page plots the points at which fminsearch evaluated your function. The second assembles the list of points into a matrix named history. When you call the example function using three outputs the third output is the history function containing all the points at which fminsearch evaluated your function.
You could write an OutputFcn that displays the points on the screen using disp or fprintf.
You could write an OutputFcn that writes the data to disk using save or fprintf.
If you have a text-to-speech function, your OutputFcn could say the coordinates to you.

Community Treasure Hunt

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

Start Hunting!