comparison scripts/optimization/fsolve.m @ 9212:6feb27c38da1

support central differences in fminunc and fsolve
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 18 May 2009 09:46:49 +0200
parents 923c7cb7f13f
children e598248a060d
comparison
equal deleted inserted replaced
9211:f0c3d3fc4903 9212:6feb27c38da1
124 ## Get default options if requested. 124 ## Get default options if requested.
125 if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults')) 125 if (nargin == 1 && ischar (fcn) && strcmp (fcn, 'defaults'))
126 x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, \ 126 x = optimset ("MaxIter", 400, "MaxFunEvals", Inf, \
127 "Jacobian", "off", "TolX", 1.5e-8, "TolFun", 1.5e-8, 127 "Jacobian", "off", "TolX", 1.5e-8, "TolFun", 1.5e-8,
128 "OutputFcn", [], "Updating", "on", "FunValCheck", "off", 128 "OutputFcn", [], "Updating", "on", "FunValCheck", "off",
129 "ComplexEqn", "off"); 129 "ComplexEqn", "off", "FinDiffType", "central");
130 return; 130 return;
131 endif 131 endif
132 132
133 if (nargin < 2 || nargin > 3 || ! ismatrix (x0)) 133 if (nargin < 2 || nargin > 3 || ! ismatrix (x0))
134 print_usage (); 134 print_usage ();
140 140
141 xsiz = size (x0); 141 xsiz = size (x0);
142 n = numel (x0); 142 n = numel (x0);
143 143
144 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on"); 144 has_jac = strcmpi (optimget (options, "Jacobian", "off"), "on");
145 cdif = strcmpi (optimget (options, "FinDiffType", "central"), "central");
145 maxiter = optimget (options, "MaxIter", 400); 146 maxiter = optimget (options, "MaxIter", 400);
146 maxfev = optimget (options, "MaxFunEvals", Inf); 147 maxfev = optimget (options, "MaxFunEvals", Inf);
147 outfcn = optimget (options, "OutputFcn"); 148 outfcn = optimget (options, "OutputFcn");
148 updating = strcmpi (optimget (options, "Updating", "on"), "on"); 149 updating = strcmpi (optimget (options, "Updating", "on"), "on");
149 complexeqn = strcmpi (optimget (options, "ComplexEqn", "off"), "on"); 150 complexeqn = strcmpi (optimget (options, "ComplexEqn", "off"), "on");
206 updating = false; 207 updating = false;
207 endif 208 endif
208 fvec = fvec(:); 209 fvec = fvec(:);
209 nfev ++; 210 nfev ++;
210 else 211 else
211 fjac = __fdjac__ (fcn, reshape (x, xsiz), fvec); 212 fjac = __fdjac__ (fcn, reshape (x, xsiz), fvec, cdif);
212 nfev += length (x); 213 nfev += (1 + cdif) * length (x);
213 endif 214 endif
214 215
215 ## For square and overdetermined systems, we update a QR 216 ## For square and overdetermined systems, we update a QR
216 ## factorization of the jacobian to avoid solving a full system in each 217 ## factorization of the jacobian to avoid solving a full system in each
217 ## step. In this case, we pass a triangular matrix to __dogleg__. 218 ## step. In this case, we pass a triangular matrix to __dogleg__.