1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution. The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 // Emulate the fminsearch command of Matlab.
13 // Search the minimum with Nelder-Mead algorithm.
14 // [x,fval,exitflag,output] = fminsearch(fun,x0,options)
16 // fun : the function to minimize
17 // x0 : a row vector with dimension n where n is the number of parameters
19 // Initial guess for optimization algorithm.
20 // options : an optional struct, as provided by optimset
22 function [x,fval,exitflag,output] = fminsearch ( varargin )
24 if rhs<>2 & rhs<>3 then
25 errmsg = sprintf("Unexpected number of arguments : %d provided while 2 or 3 are expected.",rhs);
31 // No options on the command line
33 options = optimset ("fminsearch");
35 // One options struc on the command line : use it !
36 options = varargin(3);
38 errmsg = sprintf("Unexpected error.");
41 // Compute options from the options struct
42 numberofvariables = size(x0,2);
43 // If the MaxIter option is a string, this is the default 200 value.
44 // If not, this is the actual value.
45 if type(options.MaxIter)==10 then
46 MaxIter = 200 * numberofvariables;
48 MaxIter = options.MaxIter;
50 // If the MaxFunEvals option is a string, this is the default 200 value
51 // If not, this is the actual value.
52 if type(options.MaxFunEvals)==10 then
53 MaxFunEvals = 200 * numberofvariables;
55 MaxFunEvals = options.MaxFunEvals;
57 TolFun = options.TolFun;
59 // Get options from the options struct
61 // Perform Optimization
62 nm = neldermead_new ();
63 nm = neldermead_configure(nm,"-x0",x0');
64 nm = neldermead_configure(nm,"-numberofvariables",numberofvariables);
65 nm = neldermead_configure(nm,"-simplex0method","pfeffer");
66 nm = neldermead_configure(nm,"-simplex0deltausual",0.05);
67 nm = neldermead_configure(nm,"-simplex0deltazero",0.0075);
68 nm = neldermead_configure(nm,"-method","variable");
69 nm = neldermead_configure(nm,"-function",fun);
70 nm = neldermead_configure(nm,"-maxiter",MaxIter);
71 nm = neldermead_configure(nm,"-maxfunevals",MaxFunEvals);
72 nm = neldermead_configure(nm,"-tolxmethod","disabled");
73 nm = neldermead_configure(nm,"-tolfunmethod","disabled");
74 nm = neldermead_configure(nm,"-tolssizedeltafvmethod","enabled");
75 nm = neldermead_configure(nm,"-tolsimplexizemethod","disabled");
76 nm = neldermead_configure(nm,"-toldeltafv",TolFun);
77 nm = neldermead_configure(nm,"-tolsimplexizeabsolute",TolX);
78 nm = neldermead_search(nm);
79 x = neldermead_get(nm,"-xopt")';
80 fval = neldermead_get(nm,"-fopt");
81 status = neldermead_get(nm,"-status");
85 case "maxfuneval" then
87 case "tolsizedeltafv" then
90 errmsg = sprintf("Unknown status %s",status)
98 output.algorithm = 'Nelder-Mead simplex direct search';
99 output.funcCount = neldermead_get(nm,"-funevals");
100 output.iterations = neldermead_get(nm,"-iterations");
101 output.message = sprintf("%s\n%s\n%s", "Optimization terminated:",...
102 "the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-04",...
103 "and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-04");
104 nm = neldermead_destroy(nm);