comparison scripts/optimization/fsolve.m @ 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 dacfd030633a
children 43f831758d42
comparison
equal deleted inserted replaced
8595:dee5d60257e4 8596:8833c0b18eb2
37 ## 37 ##
38 ## If @code{"Jacobian"} is @code{"on"}, it specifies that @var{fcn}, 38 ## If @code{"Jacobian"} is @code{"on"}, it specifies that @var{fcn},
39 ## called with 2 output arguments, also returns the Jacobian matrix 39 ## called with 2 output arguments, also returns the Jacobian matrix
40 ## of right-hand sides at the requested point. @code{"TolX"} specifies 40 ## of right-hand sides at the requested point. @code{"TolX"} specifies
41 ## the termination tolerance in the unknown variables, while 41 ## the termination tolerance in the unknown variables, while
42 ## @code{"TolFun"} is a tolerance for equations. Default is @code{1e1*eps} 42 ## @code{"TolFun"} is a tolerance for equations. Default is @code{1e-7}
43 ## for @code{"TolX"} and @code{1e2*eps} for @code{"TolFun"}. 43 ## for both @code{"TolX"} and @code{"TolFun"}.
44 ## If @code{"Updating"} is true, the function will attempt to use Broyden 44 ## If @code{"Updating"} is "on", the function will attempt to use Broyden
45 ## updates to update the Jacobian, in order to reduce the amount of jacobian 45 ## updates to update the Jacobian, in order to reduce the amount of jacobian
46 ## calculations. 46 ## calculations.
47 ## If your user function always calculates the Jacobian (regardless of number 47 ## If your user function always calculates the Jacobian (regardless of number
48 ## of output arguments), this option provides no advantage and should be set to 48 ## of output arguments), this option provides no advantage and should be set to
49 ## false. 49 ## false.
70 ## Note: If you only have a single nonlinear equation of one variable, using 70 ## Note: If you only have a single nonlinear equation of one variable, using
71 ## @code{fzero} is usually a much better idea. 71 ## @code{fzero} is usually a much better idea.
72 ## @seealso{fzero, optimset} 72 ## @seealso{fzero, optimset}
73 ## @end deftypefn 73 ## @end deftypefn
74 74
75 function [x, fvec, info, output, fjac] = fsolve (fcn, x0, options) 75 function [x, fvec, info, output, fjac] = fsolve (fcn, x0, options = struct ())
76 76
77 if (nargin < 3) 77 ## Get default options if requested.
78 options = struct (); 78 if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults'))
79 x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, \
80 "Jacobian", "off", "TolX", 1e-7, "TolF", 1e-7,
81 "OutputFcn", [], "Updating", "on", "FunValCheck", "off");
82 return;
83 endif
84
85 if (nargin < 2 || nargin > 3 || ! ismatrix (x0))
86 print_usage ();
87 endif
88
89 if (ischar (fcn))
90 fcn = str2func (fcn);
79 endif 91 endif
80 92
81 xsiz = size (x0); 93 xsiz = size (x0);
82 n = numel (x0); 94 n = numel (x0);
83 95
84 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on"); 96 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on");
85 maxiter = optimget (options, "MaxIter", Inf); 97 maxiter = optimget (options, "MaxIter", 400);
86 maxfev = optimget (options, "MaxFunEvals", Inf); 98 maxfev = optimget (options, "MaxFunEvals", Inf);
87 outfcn = optimget (options, "OutputFcn"); 99 outfcn = optimget (options, "OutputFcn");
88 updating = optimget (options, "Updating", true); 100 updating = strcmpi (optimget (options, "Updating", "on"), "on");
89 101
90 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); 102 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on");
91 103
92 if (funvalchk) 104 if (funvalchk)
93 ## Replace fun with a guarded version. 105 ## Replace fun with a guarded version.
97 ## These defaults are rather stringent. I think that normally, user 109 ## These defaults are rather stringent. I think that normally, user
98 ## prefers accuracy to performance. 110 ## prefers accuracy to performance.
99 111
100 macheps = eps (class (x0)); 112 macheps = eps (class (x0));
101 113
102 tolx = optimget (options, "TolX", sqrt (macheps)); 114 tolx = optimget (options, "TolX", 1e-7);
103 tolf = optimget (options, "TolFun", sqrt (macheps)); 115 tolf = optimget (options, "TolFun", 1e-7);
104 116
105 factor = 100; 117 factor = 100;
106 ## FIXME: TypicalX corresponds to user scaling (???) 118 ## FIXME: TypicalX corresponds to user scaling (???)
107 autodg = true; 119 autodg = true;
108 120