1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 2// Copyright (C) 2008-2009 - INRIA - Michael Baudin 3// Copyright (C) 2011 - DIGITEO - Michael Baudin 4// 5// Copyright (C) 2012 - 2016 - Scilab Enterprises 6// 7// This file is hereby licensed under the terms of the GNU GPL v2.0, 8// pursuant to article 5.3.4 of the CeCILL v.2.1. 9// This file was originally licensed under the terms of the CeCILL v2.1, 10// and continues to be available under such terms. 11// For more information, see the COPYING file which you should have received 12// along with this program. 13 14// <-- ENGLISH IMPOSED --> 15 16function y = rosenbrock ( x ) 17 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2; 18endfunction 19// 20// Use output function 21// 22// outfun -- 23// A sample output function 24// Arguments, input 25// x : the current point 26// optimValues : a tlist which contains the following fields 27// funccount : the number of function evaluations 28// fval : the current function value 29// iteration : the current iteration 30// procedure : a string containing the current type of step 31// state : the current state of the algorithm 32// "init", "iter", "done" 33// 34function stop = outfun ( x , optimValues , state ) 35 plot( x(1),x(2),"."); 36 // Unload all fields and check consistent values 37 fc = optimValues.funccount; 38 fv = optimValues.fval; 39 it = optimValues.iteration; 40 pr = optimValues.procedure; 41 select pr 42 case "initial simplex" 43 // OK 44 case "expand" 45 // OK 46 case "reflect" 47 // OK 48 case "shrink" 49 // OK 50 case "contract inside" 51 // OK 52 case "contract outside" 53 // OK 54 case "" 55 // OK 56 else 57 error ( sprintf ( "Unknown procedure %s." , pr ) ) 58 end 59 select state 60 case "init" 61 // OK 62 case "iter" 63 // OK 64 case "done" 65 // OK 66 else 67 error ( sprintf ( "Unknown state %s." , state ) ) 68 end 69 mprintf ( "%d %s %d -%s- %s\n" , fc , string(fv) , it , pr , state ) 70 stop = %f 71endfunction 72opt = optimset ( "OutputFcn" , outfun); 73opt = optimset ( opt , "MaxIter" , 10 ); 74[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 75close(gcf()); 76// 77// Use several output functions 78// 79function stop = outfun2 ( x , optimValues , state ) 80 global __fig1__ 81 scf ( __fig1__ ); 82 plot( x(1),x(2),"."); 83 stop = %f 84endfunction 85function stop = outfun3 ( x , optimValues , state ) 86 global __fig2__ 87 scf ( __fig2__ ); 88 plot( x(1),x(2),"o"); 89 stop = %f 90endfunction 91myfunctions = list ( outfun2 , outfun3 ); 92global __fig1__ 93global __fig2__ 94__fig1__ = scf(); 95__fig2__ = scf(); 96opt = optimset ( "OutputFcn" , myfunctions ); 97opt = optimset ( opt , "MaxIter" , 10 ); 98[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 99close(__fig1__); 100close(__fig2__); 101// 102// Use plot function 103// 104// 105// plotfun -- 106// A sample plot function 107// Arguments, input 108// x : the current point 109// optimValues : a tlist which contains the following fields 110// funcCount" : the number of function evaluations 111// fval : the current function value 112// iteration : the current iteration 113// procedure : a string containing the current type of step 114// state : the current state of the algorithm 115// "init", "iter", "done" 116// 117function plotfun ( x , optimValues , state ) 118 plot(x(1),x(2),"."); 119endfunction 120opt = optimset ( "PlotFcns" , plotfun); 121opt = optimset ( opt , "MaxIter" , 10 ); 122[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 123close(gcf()); 124// 125// Use several plot functions 126// 127function plotfun2 ( x , optimValues , state ) 128 global __fig1__ 129 scf ( __fig1__ ); 130 plot( x(1),x(2),"."); 131endfunction 132function plotfun3 ( x , optimValues , state ) 133 global __fig2__ 134 scf ( __fig2__ ); 135 plot( x(1),x(2),"o"); 136endfunction 137myfunctions = list ( plotfun2 , plotfun3 ); 138global __fig1__ 139global __fig2__ 140__fig1__ = scf(); 141__fig2__ = scf(); 142opt = optimset ( "PlotFcns" , myfunctions ); 143opt = optimset ( opt , "MaxIter" , 10 ); 144[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 145close(__fig1__); 146close(__fig2__); 147// 148// Use optimplotfval plot function 149// 150opt = optimset ( "PlotFcns" , optimplotfval ); 151opt = optimset ( opt , "MaxIter" , 10 ); 152[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 153close(gcf()); 154// 155// Use optimplotx plot function 156// 157opt = optimset ( "PlotFcns" , optimplotx ); 158opt = optimset ( opt , "MaxIter" , 10 ); 159[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 160close(gcf()); 161// 162// Use optimplotfunccount plot function 163// 164opt = optimset ( "PlotFcns" , optimplotfunccount ); 165opt = optimset ( opt , "MaxIter" , 10 ); 166[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 167close(gcf()); 168 169// 170// Use all 3 plot functions 171// 172myfunctions = list ( optimplotfval , optimplotx , optimplotfunccount ); 173opt = optimset ( "PlotFcns" , myfunctions ); 174opt = optimset ( opt , "MaxIter" , 10 ); 175[x fval] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 176close(gcf()); 177close(gcf()); 178close(gcf()); 179 180// 181// Use output function to stop the algorithm. 182// This sets the exitflag to -1. 183function stop = outfunStop ( x , optimValues , state ) 184 fv = optimValues.fval; 185 stop = ( fv < 1.e-5 ) 186endfunction 187opt = optimset ( "OutputFcn" , outfunStop); 188[x , fval , exitflag , output] = fminsearch ( rosenbrock , [-1.2 1] , opt ); 189assert_checkalmostequal ( x , [1.0 1.0], 1.e-2 ); 190assert_checktrue ( fval < 1e-5 ); 191assert_checkequal ( exitflag , -1 ); 192 193 194 195