Selective Iterative Display Print
2 views (last 30 days)
Show older comments
Hello,
I am using optimoptions and iterative display to print the iterations of a specific function. However, I want to only print the first 10 and last 10. Here is my current code
opt = optimoptions(@fminunc,'Display','iter-detailed','Algorithm',...
'quasi-newton','HessUpdate','steepdesc','MaxFunctionEvaluations',2000);
fun = @(x) 5*x(1).^2+x(2).^2+4*x(1)*x(2)-14*x(1)-6*x(2)+20;
x0 = [0 10]';
[iter, x, fval, normofstep] = fminunc(fun, x0, opt);
I was wondering how I can only print only the first and last ten since this returns 202 iterations.
Thank you
0 Comments
Answers (2)
Walter Roberson
on 6 Nov 2020
There is no method provided for that. To implement it, the code would have to keep a FIFO buffer of the iteration results, throwing away the oldest result as each new result came in after the 20th, and then when it detects that it is ready to terminate, it would have to print all the results at the same time. There is no provision for this.
If you somehow knew ahead of time exactly how many iterations it was going to take, you could add an outputfcn that tested the iteration number and did not start printing until it was close enough to what you (somehow) knew the end was going to be.
Or you could evalc() to write the results into a buffer, and after it was done, filter out the parts you do not want, with the user not seeing anything until after the filtering was sone.
0 Comments
Alan Weiss
on 6 Nov 2020
I believe that you could do this using an output function. For syntax details, see Output Function and Plot Function Syntax.
- When state = 'init', print out the iterative display headers manually.
- When the state = 'iter', and state.iteration is less than 11 or more than 191, print the iterative display items you want.
It's a lot of work to code it properly, but it can be done. For sample code, edit fminunc and in that code, search for and then highlight fminsub and press control+D.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment
Walter Roberson
on 6 Nov 2020
... "If you somehow knew ahead of time exactly how many iterations it was going to take"
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!