Mercurial > hg > octave-nkf
changeset 8596:8833c0b18eb2
enable default settings queries in optim funcs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 27 Jan 2009 08:15:08 +0100 |
parents | dee5d60257e4 |
children | c86718093c1b |
files | scripts/ChangeLog scripts/optimization/fsolve.m scripts/optimization/fzero.m scripts/optimization/optimset.m |
diffstat | 4 files changed, 39 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,10 @@ +2009-01-27 Jaroslav Hajek <highegg@gmail.com> + + * 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 <jason@acm.org> * miscellaneous/orderfields.m: Also avoid loop for non-empty structs.
--- a/scripts/optimization/fsolve.m +++ b/scripts/optimization/fsolve.m @@ -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 (???)
--- a/scripts/optimization/fzero.m +++ b/scripts/optimization/fzero.m @@ -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
--- a/scripts/optimization/optimset.m +++ b/scripts/optimization/optimset.m @@ -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}))