This is a static copy of a profile report

Home

fminsearch (870 calls, 3.596 sec)
Generated 05-Aug-2011 13:03:52 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/funfun/fminsearch.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
calc_loadcorrect_finalfunction10
fitOneOverFfunction860
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
306
x(:) = xr; fxr = funfcn(x,vara...
100820.601 s16.7%
347
x(:) = xcc; fxcc = funfcn(x,va...
82440.525 s14.6%
368
[fv,j] = sort(fv);
100820.142 s4.0%
105
maxfun = optimget(options,'Max...
8700.109 s3.0%
312
x(:) = xe; fxe = funfcn(x,vara...
13250.098 s2.7%
All other lines  2.120 s59.0%
Totals  3.596 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
fitOneOverF>@(y)residualWhite(y)anonymous function215360.853 s23.7%
optimgetfunction69600.590 s16.4%
...final>@(x)(std((sky_res-x*load_res)))anonymous function3720.273 s7.6%
fcnchkfunction8700.055 s1.5%
Self time (built-ins, overhead, etc.)  1.825 s50.8%
Totals  3.596 s100% 
Code Analyzer results
Line numberMessage
174{ A{:} B } can often be replaced by [ A {B}], which can be much faster.
222Terminate statement with semicolon to suppress output (in functions).
223Terminate statement with semicolon to suppress output (in functions).
224Terminate statement with semicolon to suppress output (in functions).
267Terminate statement with semicolon to suppress output (in functions).
268Terminate statement with semicolon to suppress output (in functions).
269Terminate statement with semicolon to suppress output (in functions).
283The value assigned to variable 'exitflag' might be unused.
376Terminate statement with semicolon to suppress output (in functions).
377Terminate statement with semicolon to suppress output (in functions).
378Terminate statement with semicolon to suppress output (in functions).
Coverage results
[ Show coverage for parent directory ]
Total lines in function441
Non-code lines (comments, blank lines)162
Code lines (lines that can run)279
Code lines that did run146
Code lines that did not run133
Coverage (did run/can run)52.33 %
Function listing
   time   calls  line
1 function [x,fval,exitflag,output] = fminsearch(funfcn,x,options,varargin)
2 %FMINSEARCH Multidimensional unconstrained nonlinear minimization (Nelder-Mead).
3 % X = FMINSEARCH(FUN,X0) starts at X0 and attempts to find a local minimizer
4 % X of the function FUN. FUN is a function handle. FUN accepts input X and
5 % returns a scalar function value F evaluated at X. X0 can be a scalar, vector
6 % or matrix.
7 %
8 % X = FMINSEARCH(FUN,X0,OPTIONS) minimizes with the default optimization
9 % parameters replaced by values in the structure OPTIONS, created
10 % with the OPTIMSET function. See OPTIMSET for details. FMINSEARCH uses
11 % these options: Display, TolX, TolFun, MaxFunEvals, MaxIter, FunValCheck,
12 % PlotFcns, and OutputFcn.
13 %
14 % X = FMINSEARCH(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
15 % structure with the function FUN in PROBLEM.objective, the start point
16 % in PROBLEM.x0, the options structure in PROBLEM.options, and solver
17 % name 'fminsearch' in PROBLEM.solver. The PROBLEM structure must have
18 % all the fields.
19 %
20 % [X,FVAL]= FMINSEARCH(...) returns the value of the objective function,
21 % described in FUN, at X.
22 %
23 % [X,FVAL,EXITFLAG] = FMINSEARCH(...) returns an EXITFLAG that describes
24 % the exit condition of FMINSEARCH. Possible values of EXITFLAG and the
25 % corresponding exit conditions are
26 %
27 % 1 Maximum coordinate difference between current best point and other
28 % points in simplex is less than or equal to TolX, and corresponding
29 % difference in function values is less than or equal to TolFun.
30 % 0 Maximum number of function evaluations or iterations reached.
31 % -1 Algorithm terminated by the output function.
32 %
33 % [X,FVAL,EXITFLAG,OUTPUT] = FMINSEARCH(...) returns a structure
34 % OUTPUT with the number of iterations taken in OUTPUT.iterations, the
35 % number of function evaluations in OUTPUT.funcCount, the algorithm name
36 % in OUTPUT.algorithm, and the exit message in OUTPUT.message.
37 %
38 % Examples
39 % FUN can be specified using @:
40 % X = fminsearch(@sin,3)
41 % finds a minimum of the SIN function near 3.
42 % In this case, SIN is a function that returns a scalar function value
43 % SIN evaluated at X.
44 %
45 % FUN can be an anonymous function:
46 % X = fminsearch(@(x) norm(x),[1;2;3])
47 % returns a point near the minimizer [0;0;0].
48 %
49 % FUN can be a parameterized function. Use an anonymous function to
50 % capture the problem-dependent parameters:
51 % f = @(x,c) x(1).^2+c.*x(2).^2; % The parameterized function.
52 % c = 1.5; % The parameter.
53 % X = fminsearch(@(x) f(x,c),[0.3;1])
54 %
55 % FMINSEARCH uses the Nelder-Mead simplex (direct search) method.
56 %
57 % See also OPTIMSET, FMINBND, FUNCTION_HANDLE.
58
59 % Reference: Jeffrey C. Lagarias, James A. Reeds, Margaret H. Wright,
60 % Paul E. Wright, "Convergence Properties of the Nelder-Mead Simplex
61 % Method in Low Dimensions", SIAM Journal of Optimization, 9(1):
62 % p.112-147, 1998.
63
64 % Copyright 1984-2010 The MathWorks, Inc.
65 % $Revision: 1.21.4.21 $ $Date: 2010/11/22 02:46:05 $
66
67
0.05 870 68 defaultopt = struct('Display','notify','MaxIter','200*numberOfVariables',...
69 'MaxFunEvals','200*numberOfVariables','TolX',1e-4,'TolFun',1e-4, ...
70 'FunValCheck','off','OutputFcn',[],'PlotFcns',[]);
71
72 % If just 'defaults' passed in, return the default options in X
870 73 if nargin==1 && nargout <= 1 && isequal(funfcn,'defaults')
74 x = defaultopt;
75 return
76 end
77
0.03 870 78 if nargin<3, options = []; end
79
80 % Detect problem structure input
0.01 870 81 if nargin == 1
82 if isa(funfcn,'struct')
83 [funfcn,x,options] = separateOptimStruct(funfcn);
84 else % Single input and non-structure
85 error(message('MATLAB:fminsearch:InputArg'));
86 end
87 end
88
870 89 if nargin == 0
90 error(message('MATLAB:fminsearch:NotEnoughInputs'));
91 end
92
93
94 % Check for non-double inputs
870 95 if ~isa(x,'double')
96 error(message('MATLAB:fminsearch:NonDoubleInput'))
97 end
98
870 99 n = numel(x);
870 100 numberOfVariables = n;
101
0.07 870 102 printtype = optimget(options,'Display',defaultopt,'fast');
0.10 870 103 tolx = optimget(options,'TolX',defaultopt,'fast');
0.03 870 104 tolf = optimget(options,'TolFun',defaultopt,'fast');
0.11 870 105 maxfun = optimget(options,'MaxFunEvals',defaultopt,'fast');
0.08 870 106 maxiter = optimget(options,'MaxIter',defaultopt,'fast');
0.10 870 107 funValCheck = strcmp(optimget(options,'FunValCheck',defaultopt,'fast'),'on');
108
109 % In case the defaults were gathered from calling: optimset('fminsearch'):
870 110 if ischar(maxfun)
10 111 if isequal(lower(maxfun),'200*numberofvariables')
10 112 maxfun = 200*numberOfVariables;
113 else
114 error(message('MATLAB:fminsearch:OptMaxFunEvalsNotInteger'))
115 end
10 116 end
870 117 if ischar(maxiter)
10 118 if isequal(lower(maxiter),'200*numberofvariables')
10 119 maxiter = 200*numberOfVariables;
120 else
121 error(message('MATLAB:fminsearch:OptMaxIterNotInteger'))
122 end
10 123 end
124
870 125 switch printtype
870 126 case {'notify','notify-detailed'}
10 127 prnt = 1;
0.01 860 128 case {'none','off'}
860 129 prnt = 0;
130 case {'iter','iter-detailed'}
131 prnt = 3;
132 case {'final','final-detailed'}
133 prnt = 2;
134 case 'simplex'
135 prnt = 4;
136 otherwise
137 prnt = 1;
138 end
139 % Handle the output
0.10 870 140 outputfcn = optimget(options,'OutputFcn',defaultopt,'fast');
870 141 if isempty(outputfcn)
870 142 haveoutputfcn = false;
143 else
144 haveoutputfcn = true;
145 xOutputfcn = x; % Last x passed to outputfcn; has the input x's shape
146 % Parse OutputFcn which is needed to support cell array syntax for OutputFcn.
147 outputfcn = createCellArrayOfFunctions(outputfcn,'OutputFcn');
148 end
149
150 % Handle the plot
0.08 870 151 plotfcns = optimget(options,'PlotFcns',defaultopt,'fast');
0.01 870 152 if isempty(plotfcns)
870 153 haveplotfcn = false;
154 else
155 haveplotfcn = true;
156 xOutputfcn = x; % Last x passed to plotfcns; has the input x's shape
157 % Parse PlotFcns which is needed to support cell array syntax for PlotFcns.
158 plotfcns = createCellArrayOfFunctions(plotfcns,'PlotFcns');
159 end
160
870 161 header = ' Iteration Func-count min f(x) Procedure';
162
163 % Convert to function handle as needed.
0.08 870 164 funfcn = fcnchk(funfcn,length(varargin));
165 % Add a wrapper function to check for Inf/NaN/complex values
870 166 if funValCheck
167 % Add a wrapper function, CHECKFUN, to check for NaN/complex values without
168 % having to change the calls that look like this:
169 % f = funfcn(x,varargin{:});
170 % x is the first argument to CHECKFUN, then the user's function,
171 % then the elements of varargin. To accomplish this we need to add the
172 % user's function to the beginning of varargin, and change funfcn to be
173 % CHECKFUN.
174 varargin = {funfcn, varargin{:}};
175 funfcn = @checkfun;
176 end
177
870 178 n = numel(x);
179
180 % Initialize parameters
870 181 rho = 1; chi = 2; psi = 0.5; sigma = 0.5;
870 182 onesn = ones(1,n);
0.01 870 183 two2np1 = 2:n+1;
870 184 one2n = 1:n;
185
186 % Set up a simplex near the initial guess.
870 187 xin = x(:); % Force xin to be a column vector
870 188 v = zeros(n,n+1); fv = zeros(1,n+1);
870 189 v(:,1) = xin; % Place input guess in the simplex! (credit L.Pfeffer at Stanford)
870 190 x(:) = xin; % Change x to the form expected by funfcn
0.04 870 191 fv(:,1) = funfcn(x,varargin{:});
870 192 func_evals = 1;
870 193 itercount = 0;
870 194 how = '';
195 % Initial simplex setup continues later
196
197 % Initialize the output and plot functions.
870 198 if haveoutputfcn || haveplotfcn
199 [xOutputfcn, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'init',itercount, ...
200 func_evals, how, fv(:,1),varargin{:});
201 if stop
202 [x,fval,exitflag,output] = cleanUpInterrupt(xOutputfcn,optimValues);
203 if prnt > 0
204 disp(output.message)
205 end
206 return;
207 end
208 end
209
210 % Print out initial f(x) as 0th iteration
870 211 if prnt == 3
212 disp(' ')
213 disp(header)
214 fprintf(' %5.0f %5.0f %12.6g %s\n', itercount, func_evals, fv(1), how);
870 215 elseif prnt == 4
216 clc
217 formatsave = get(0,{'format','formatspacing'});
218 format compact
219 format short e
220 disp(' ')
221 disp(how)
222 v
223 fv
224 func_evals
225 end
226 % OutputFcn and PlotFcns call
0.01 870 227 if haveoutputfcn || haveplotfcn
228 [xOutputfcn, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'iter',itercount, ...
229 func_evals, how, fv(:,1),varargin{:});
230 if stop % Stop per user request.
231 [x,fval,exitflag,output] = cleanUpInterrupt(xOutputfcn,optimValues);
232 if prnt > 0
233 disp(output.message)
234 end
235 return;
236 end
237 end
238
239 % Continue setting up the initial simplex.
240 % Following improvement suggested by L.Pfeffer at Stanford
870 241 usual_delta = 0.05; % 5 percent deltas for non-zero terms
0.01 870 242 zero_term_delta = 0.00025; % Even smaller delta for zero elements of x
870 243 for j = 1:n
870 244 y = xin;
870 245 if y(j) ~= 0
0.01 870 246 y(j) = (1 + usual_delta)*y(j);
247 else
248 y(j) = zero_term_delta;
249 end
0.02 870 250 v(:,j+1) = y;
0.05 870 251 x(:) = y; f = funfcn(x,varargin{:});
870 252 fv(1,j+1) = f;
870 253 end
254
255 % sort so v(1,:) has the lowest function value
0.02 870 256 [fv,j] = sort(fv);
0.01 870 257 v = v(:,j);
258
870 259 how = 'initial simplex';
870 260 itercount = itercount + 1;
870 261 func_evals = n+1;
870 262 if prnt == 3
263 fprintf(' %5.0f %5.0f %12.6g %s\n', itercount, func_evals, fv(1), how)
870 264 elseif prnt == 4
265 disp(' ')
266 disp(how)
267 v
268 fv
269 func_evals
270 end
271 % OutputFcn and PlotFcns call
870 272 if haveoutputfcn || haveplotfcn
273 [xOutputfcn, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'iter',itercount, ...
274 func_evals, how, fv(:,1),varargin{:});
275 if stop % Stop per user request.
276 [x,fval,exitflag,output] = cleanUpInterrupt(xOutputfcn,optimValues);
277 if prnt > 0
278 disp(output.message)
279 end
280 return;
281 end
282 end
870 283 exitflag = 1;
284
285 % Main algorithm: iterate until
286 % (a) the maximum coordinate difference between the current best point and the
287 % other points in the simplex is less than or equal to TolX. Specifically,
288 % until max(||v2-v1||,||v2-v1||,...,||v(n+1)-v1||) <= TolX,
289 % where ||.|| is the infinity-norm, and v1 holds the
290 % vertex with the current lowest value; AND
291 % (b) the corresponding difference in function values is less than or equal
292 % to TolFun. (Cannot use OR instead of AND.)
293 % The iteration stops if the maximum number of iterations or function evaluations
294 % are exceeded
0.02 870 295 while func_evals < maxfun && itercount < maxiter
0.07 10952 296 if max(abs(fv(1)-fv(two2np1))) <= max(tolf,10*eps(fv(1))) && ...
297 max(max(abs(v(:,two2np1)-v(:,onesn)))) <= max(tolx,10*eps(max(v(:,1))))
0.02 870 298 break
299 end
300
301 % Compute the reflection point
302
303 % xbar = average of the n (NOT n+1) best points
0.10 10082 304 xbar = sum(v(:,one2n), 2)/n;
0.07 10082 305 xr = (1 + rho)*xbar - rho*v(:,end);
0.60 10082 306 x(:) = xr; fxr = funfcn(x,varargin{:});
0.04 10082 307 func_evals = func_evals+1;
308
0.04 10082 309 if fxr < fv(:,1)
310 % Calculate the expansion point
0.01 1325 311 xe = (1 + rho*chi)*xbar - rho*chi*v(:,end);
0.10 1325 312 x(:) = xe; fxe = funfcn(x,varargin{:});
1325 313 func_evals = func_evals+1;
1325 314 if fxe < fxr
0.01 927 315 v(:,end) = xe;
927 316 fv(:,end) = fxe;
927 317 how = 'expand';
398 318 else
398 319 v(:,end) = xr;
398 320 fv(:,end) = fxr;
398 321 how = 'reflect';
398 322 end
0.01 8757 323 else % fv(:,1) <= fxr
0.07 8757 324 if fxr < fv(:,n)
325 v(:,end) = xr;
326 fv(:,end) = fxr;
327 how = 'reflect';
8757 328 else % fxr >= fv(:,n)
329 % Perform contraction
0.08 8757 330 if fxr < fv(:,end)
331 % Perform an outside contraction
0.03 513 332 xc = (1 + psi*rho)*xbar - psi*rho*v(:,end);
0.05 513 333 x(:) = xc; fxc = funfcn(x,varargin{:});
513 334 func_evals = func_evals+1;
335
513 336 if fxc <= fxr
513 337 v(:,end) = xc;
513 338 fv(:,end) = fxc;
513 339 how = 'contract outside';
340 else
341 % perform a shrink
342 how = 'shrink';
343 end
0.01 8244 344 else
345 % Perform an inside contraction
0.05 8244 346 xcc = (1-psi)*xbar + psi*v(:,end);
0.52 8244 347 x(:) = xcc; fxcc = funfcn(x,varargin{:});
0.02 8244 348 func_evals = func_evals+1;
349
0.03 8244 350 if fxcc < fv(:,end)
0.02 8240 351 v(:,end) = xcc;
0.03 8240 352 fv(:,end) = fxcc;
0.01 8240 353 how = 'contract inside';
4 354 else
355 % perform a shrink
4 356 how = 'shrink';
4 357 end
0.02 8244 358 end
0.07 8757 359 if strcmp(how,'shrink')
4 360 for j=two2np1
4 361 v(:,j)=v(:,1)+sigma*(v(:,j) - v(:,1));
4 362 x(:) = v(:,j); fv(:,j) = funfcn(x,varargin{:});
4 363 end
4 364 func_evals = func_evals + n;
4 365 end
0.01 8757 366 end
0.02 8757 367 end
0.14 10082 368 [fv,j] = sort(fv);
0.04 10082 369 v = v(:,j);
0.01 10082 370 itercount = itercount + 1;
0.01 10082 371 if prnt == 3
372 fprintf(' %5.0f %5.0f %12.6g %s\n', itercount, func_evals, fv(1), how)
0.02 10082 373 elseif prnt == 4
374 disp(' ')
375 disp(how)
376 v
377 fv
378 func_evals
379 end
380 % OutputFcn and PlotFcns call
0.04 10082 381 if haveoutputfcn || haveplotfcn
382 [xOutputfcn, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'iter',itercount, ...
383 func_evals, how, fv(:,1),varargin{:});
384 if stop % Stop per user request.
385 [x,fval,exitflag,output] = cleanUpInterrupt(xOutputfcn,optimValues);
386 if prnt > 0
387 disp(output.message)
388 end
389 return;
390 end
391 end
0.04 10082 392 end % while
393
0.01 870 394 x(:) = v(:,1);
870 395 fval = fv(:,1);
396
870 397 if prnt == 4,
398 % reset format
399 set(0,{'format','formatspacing'},formatsave);
400 end
870 401 output.iterations = itercount;
0.01 870 402 output.funcCount = func_evals;
870 403 output.algorithm = 'Nelder-Mead simplex direct search';
404
405 % OutputFcn and PlotFcns call
870 406 if haveoutputfcn || haveplotfcn
407 callOutputAndPlotFcns(outputfcn,plotfcns,x,xOutputfcn,'done',itercount, func_evals, how, fval, varargin{:});
408 end
409
870 410 if func_evals >= maxfun
411 msg = sprintf(['Exiting: Maximum number of function evaluations has been exceeded\n' ...
412 ' - increase MaxFunEvals option.\n' ...
413 ' Current function value: %f \n'], fval);
414 if prnt > 0
415 disp(' ')
416 disp(msg)
417 end
418 exitflag = 0;
870 419 elseif itercount >= maxiter
420 msg = sprintf(['Exiting: Maximum number of iterations has been exceeded\n' ...
421 ' - increase MaxIter option.\n' ...
422 ' Current function value: %f \n'], fval);
423 if prnt > 0
424 disp(' ')
425 disp(msg)
426 end
427 exitflag = 0;
870 428 else
0.03 870 429 msg = ...
430 sprintf(['Optimization terminated:\n', ...
431 ' the current x satisfies the termination criteria using OPTIONS.TolX of %e \n' ...
432 ' and F(X) satisfies the convergence criteria using OPTIONS.TolFun of %e \n'], ...
433 tolx, tolf);
870 434 if prnt > 1
435 disp(' ')
436 disp(msg)
437 end
870 438 exitflag = 1;
870 439 end
440
870 441 output.message = msg;

Other subfunctions in this file are not included in this listing.