Selective Iterative Display Print

조회 수: 2(최근 30일)
Braden Kerr
Braden Kerr 2020년 11월 6일
댓글: Walter Roberson 2020년 11월 6일
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',...
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


Walter Roberson
Walter Roberson 2020년 11월 6일
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.

Alan Weiss
Alan Weiss 2020년 11월 6일
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
Walter Roberson
Walter Roberson 2020년 11월 6일
... "If you somehow knew ahead of time exactly how many iterations it was going to take"

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by