Selective Iterative Display Print

2 Ansichten (letzte 30 Tage)
Braden Kerr
Braden Kerr am 6 Nov. 2020
Kommentiert: Walter Roberson am 6 Nov. 2020
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

Antworten (2)

Walter Roberson
Walter Roberson am 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.

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by