# HG changeset patch # User Jaroslav Hajek # Date 1233040508 -3600 # Node ID 8833c0b18eb2f82cf1ec6b20366fb3a78ca8570e # Parent dee5d60257e47855bbe56bd00c385d79de04f572 enable default settings queries in optim funcs diff -r dee5d60257e4 -r 8833c0b18eb2 scripts/ChangeLog --- a/scripts/ChangeLog Mon Jan 26 23:07:09 2009 -0500 +++ b/scripts/ChangeLog Tue Jan 27 08:15:08 2009 +0100 @@ -1,3 +1,10 @@ +2009-01-27 Jaroslav Hajek + + * optimization/fsolve.m: Provide default values on request. + Adjust some defaults. + * optimization/fzero.m: Dtto. + * optimization/optimset.m: Query optimal values via the M*b way. + 2009-01-26 Jason Riedy * miscellaneous/orderfields.m: Also avoid loop for non-empty structs. diff -r dee5d60257e4 -r 8833c0b18eb2 scripts/optimization/fsolve.m --- a/scripts/optimization/fsolve.m Mon Jan 26 23:07:09 2009 -0500 +++ b/scripts/optimization/fsolve.m Tue Jan 27 08:15:08 2009 +0100 @@ -39,9 +39,9 @@ ## called with 2 output arguments, also returns the Jacobian matrix ## of right-hand sides at the requested point. @code{"TolX"} specifies ## the termination tolerance in the unknown variables, while -## @code{"TolFun"} is a tolerance for equations. Default is @code{1e1*eps} -## for @code{"TolX"} and @code{1e2*eps} for @code{"TolFun"}. -## If @code{"Updating"} is true, the function will attempt to use Broyden +## @code{"TolFun"} is a tolerance for equations. Default is @code{1e-7} +## for both @code{"TolX"} and @code{"TolFun"}. +## If @code{"Updating"} is "on", the function will attempt to use Broyden ## updates to update the Jacobian, in order to reduce the amount of jacobian ## calculations. ## If your user function always calculates the Jacobian (regardless of number @@ -72,20 +72,32 @@ ## @seealso{fzero, optimset} ## @end deftypefn -function [x, fvec, info, output, fjac] = fsolve (fcn, x0, options) +function [x, fvec, info, output, fjac] = fsolve (fcn, x0, options = struct ()) - if (nargin < 3) - options = struct (); + ## Get default options if requested. + if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults')) + x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, \ + "Jacobian", "off", "TolX", 1e-7, "TolF", 1e-7, + "OutputFcn", [], "Updating", "on", "FunValCheck", "off"); + return; + endif + + if (nargin < 2 || nargin > 3 || ! ismatrix (x0)) + print_usage (); + endif + + if (ischar (fcn)) + fcn = str2func (fcn); endif xsiz = size (x0); n = numel (x0); has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on"); - maxiter = optimget (options, "MaxIter", Inf); + maxiter = optimget (options, "MaxIter", 400); maxfev = optimget (options, "MaxFunEvals", Inf); outfcn = optimget (options, "OutputFcn"); - updating = optimget (options, "Updating", true); + updating = strcmpi (optimget (options, "Updating", "on"), "on"); funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); @@ -99,8 +111,8 @@ macheps = eps (class (x0)); - tolx = optimget (options, "TolX", sqrt (macheps)); - tolf = optimget (options, "TolFun", sqrt (macheps)); + tolx = optimget (options, "TolX", 1e-7); + tolf = optimget (options, "TolFun", 1e-7); factor = 100; ## FIXME: TypicalX corresponds to user scaling (???) diff -r dee5d60257e4 -r 8833c0b18eb2 scripts/optimization/fzero.m --- a/scripts/optimization/fzero.m Mon Jan 26 23:07:09 2009 -0500 +++ b/scripts/optimization/fzero.m Tue Jan 27 08:15:08 2009 +0100 @@ -61,6 +61,13 @@ function [x, fval, info, output] = fzero (fun, x0, options = struct ()) + ## Get default options if requested. + if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) + x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 0, \ + "OutputFcn", [], "FunValCheck", "off"); + return; + endif + if (nargin < 2 || nargin > 3) print_usage (); endif diff -r dee5d60257e4 -r 8833c0b18eb2 scripts/optimization/optimset.m --- a/scripts/optimization/optimset.m Mon Jan 26 23:07:09 2009 -0500 +++ b/scripts/optimization/optimset.m Tue Jan 27 08:15:08 2009 +0100 @@ -52,10 +52,9 @@ elseif (nargs == 1 && ischar (varargin{1})) ## Return defaults for named function. fcn = varargin{1}; - optfcn = sprintf ("__%s_defopts__", fcn); - if (exist (optfcn)) - retval = optimset (struct (), feval (optfcn)); - else + try + retval = feval (fcn, 'defaults'); + catch error ("no defaults for function `%s'", fcn); endif elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2}))