This is a static copy of a profile report

Home

optimfcnchk (860 calls, 0.317 sec)
Generated 05-Aug-2011 13:03:52 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/shared/optimlib/optimfcnchk.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
fminconfunction860
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
289
[funfcn, idandmsg] = fcnchk(fu...
8600.153 s48.3%
295
if ~isempty(idandmsg)
8600.022 s6.9%
73
if isequal(caller,'fseminf')
8600.022 s6.9%
319
allfcns{2} = caller;
8600.011 s3.4%
318
allfcns{1} = calltype;
8600.011 s3.4%
All other lines  0.098 s31.0%
Totals  0.317 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
fcnchkfunction8600.142 s44.8%
Self time (built-ins, overhead, etc.)  0.175 s55.2%
Totals  0.317 s100% 
Code Analyzer results
Line numberMessage
369Use && instead of & as the AND operator in (scalar) conditional statements.
Coverage results
[ Show coverage for parent directory ]
Total lines in function417
Non-code lines (comments, blank lines)176
Code lines (lines that can run)241
Code lines that did run45
Code lines that did not run196
Coverage (did run/can run)18.67 %
Function listing
   time   calls  line
1 function [allfcns,idandmsg] = optimfcnchk(funstr,caller,lenVarIn,funValCheck, ...
2 gradflag,hessflag,constrflag,Algorithm,ntheta)
3 %OPTIMFCNCHK Pre- and post-process function expression for FCNCHK.
4 %
5 % This is a helper function.
6
7 % [ALLFCNS,idandmsg] = OPTIMFCNCHK(FUNSTR,CALLER,lenVarIn,GRADFLAG) takes
8 % the (nonempty) function handle or expression FUNSTR from CALLER with
9 % lenVarIn extra arguments, parses it according to what CALLER is, then
10 % returns a string or inline object in ALLFCNS. If an error occurs,
11 % this message is put in idandmsg.
12 %
13 % ALLFCNS is a cell array:
14 % ALLFCNS{1} contains a flag
15 % that says if the objective and gradients are together in one function
16 % (calltype=='fungrad') or in two functions (calltype='fun_then_grad')
17 % or there is no gradient (calltype=='fun'), etc.
18 % ALLFCNS{2} contains the string CALLER.
19 % ALLFCNS{3} contains the objective (or constraint) function
20 % ALLFCNS{4} contains the gradient function
21 % ALLFCNS{5} contains the hessian function (not used for constraint function).
22 %
23 % If funValCheck is 'on', then we update the funfcn's (fun/grad/hess) so
24 % they are called through CHECKFUN to check for NaN's, Inf's, or complex
25 % values. Add a wrapper function, CHECKFUN, to check for NaN/complex
26 % values without having to change the calls that look like this:
27 % f = funfcn(x,varargin{:});
28 % CHECKFUN is a nested function so we can get the 'caller', 'userfcn', and
29 % 'ntheta' (for fseminf constraint functions) information from this function
30 % and the user's function and CHECKFUN can both be called with the same
31 % arguments.
32
33 % NOTE: we assume FUNSTR is nonempty.
34
35 % Copyright 1990-2010 The MathWorks, Inc.
36 % $Revision: 1.1.4.5 $ $Date: 2010/10/08 17:17:23 $
37
38 % Initialize
0.01 860 39 if nargin < 9
860 40 ntheta = 0;
0.01 860 41 if nargin < 8
42 Algorithm = [];
43 if nargin < 7
44 constrflag = false;
45 if nargin < 6
46 hessflag = false;
47 if nargin < 5
48 gradflag = false;
49 end
50 end
51 end
52 end
860 53 end
860 54 if constrflag
55 graderrid = 'optimlib:optimfcnchk:NoConstraintGradientFunction';
56 graderrmsg = 'Constraint gradient function expected (OPTIONS.GradConstr=''on'') but not found.';
57 warnid = 'optimlib:optimfcnchk:ConstraintGradientOptionOff';
58 warnstr = ...
59 sprintf(['Constraint gradient function provided but OPTIONS.GradConstr=''off'';', ...
60 ' ignoring constraint gradient function and using finite-differencing.', ...
61 ' Rerun with OPTIONS.GradConstr=''on'' to use constraint gradient function.']);
860 62 else
860 63 graderrid = 'optimlib:optimfcnchk:NoGradientFunction';
860 64 graderrmsg = 'Gradient function expected (OPTIONS.GradObj=''on'') but not found.';
860 65 warnid = 'optimlib:optimfcnchk:GradientOptionOff';
0.01 860 66 warnstr = ...
67 sprintf(['Gradient function provided but OPTIONS.GradObj=''off'';', ...
68 ' ignoring gradient function and using finite-differencing.', ...
69 ' Rerun with OPTIONS.GradObj=''on'' to use gradient function.']);
70
860 71 end
860 72 idandmsg='';
0.02 860 73 if isequal(caller,'fseminf')
74 nonlconmsg = 'SEMINFCON must be a function.';
75 nonlconid = 'optimlib:optimfcnchk:SeminfconNotAFunction';
860 76 else
860 77 nonlconmsg = 'NONLCON must be a function.';
860 78 nonlconid = 'optimlib:optimfcnchk:NonlconNotAFunction';
860 79 end
860 80 allfcns = {};
860 81 funfcn = [];
860 82 gradfcn = [];
860 83 hessfcn = [];
860 84 if gradflag && hessflag
85 if strcmpi(caller,'fmincon') && strcmpi(Algorithm,'interior-point')
86 % fmincon interior-point doesn't take Hessian as 3rd argument
87 % of objective function - it's passed as a separate function
88 calltype = 'fungrad';
89 else
90 calltype = 'fungradhess';
91 end
860 92 elseif gradflag
93 calltype = 'fungrad';
860 94 else % ~gradflag & ~hessflag, OR ~gradflag & hessflag: this problem handled later
860 95 calltype = 'fun';
860 96 end
97
860 98 if isa(funstr, 'cell') && length(funstr)==1 % {fun}
99 % take the cellarray apart: we know it is nonempty
100 if gradflag
101 error(graderrid,graderrmsg)
102 end
103 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
104 % Insert call to nested function checkfun which calls user funfcn
105 if funValCheck
106 userfcn = funfcn;
107 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
108 end
109 if ~isempty(idandmsg)
110 if constrflag % Constraint, not objective, function, so adjust error message
111 error(nonlconid,nonlconmsg);
112 else
113 error(idandmsg);
114 end
115 end
860 116 elseif isa(funstr, 'cell') && length(funstr)==2 && isempty(funstr{2}) % {fun,[]}
117 if gradflag
118 error(graderrid,graderrmsg)
119 end
120 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
121 if funValCheck
122 userfcn = funfcn;
123 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
124 end
125 if ~isempty(idandmsg)
126 if constrflag
127 error(nonlconid,nonlconmsg);
128 else
129 error(idandmsg);
130 end
131 end
132
860 133 elseif isa(funstr, 'cell') && length(funstr)==2 % {fun, grad} and ~isempty(funstr{2})
134
135 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
136 if funValCheck
137 userfcn = funfcn;
138 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
139 end
140
141 if ~isempty(idandmsg)
142 if constrflag
143 error(nonlconid,nonlconmsg);
144 else
145 error(idandmsg);
146 end
147 end
148 [gradfcn, idandmsg] = fcnchk(funstr{2},lenVarIn);
149 if funValCheck
150 userfcn = gradfcn;
151 gradfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
152 end
153 if ~isempty(idandmsg)
154 if constrflag
155 error(nonlconid,nonlconmsg);
156 else
157 error(idandmsg);
158 end
159 end
160 calltype = 'fun_then_grad';
161 if ~gradflag
162 warning(warnid,warnstr);
163 calltype = 'fun';
164 end
860 165 elseif isa(funstr, 'cell') && length(funstr)==3 ...
166 && ~isempty(funstr{1}) && isempty(funstr{2}) && isempty(funstr{3}) % {fun, [], []}
167 if gradflag
168 error(graderrid,graderrmsg)
169 end
170 if hessflag
171 error(message('optimlib:optimfcnchk:NoHessianFunction'))
172 end
173 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
174 if funValCheck
175 userfcn = funfcn;
176 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
177 end
178 if ~isempty(idandmsg)
179 if constrflag
180 error(nonlconid,nonlconmsg);
181 else
182 error(idandmsg);
183 end
184 end
185
0.01 860 186 elseif isa(funstr, 'cell') && length(funstr)==3 ...
187 && ~isempty(funstr{2}) && ~isempty(funstr{3}) % {fun, grad, hess}
188 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
189 if funValCheck
190 userfcn = funfcn;
191 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
192 end
193
194 if ~isempty(idandmsg)
195 if constrflag
196 error(nonlconid,nonlconmsg);
197 else
198 error(idandmsg);
199 end
200 end
201 [gradfcn, idandmsg] = fcnchk(funstr{2},lenVarIn);
202 if funValCheck
203 userfcn = gradfcn;
204 gradfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
205 end
206
207 if ~isempty(idandmsg)
208 if constrflag
209 error(nonlconid,nonlconmsg);
210 else
211 error(idandmsg);
212 end
213 end
214 [hessfcn, idandmsg] = fcnchk(funstr{3},lenVarIn);
215 if funValCheck
216 userfcn = hessfcn;
217 hessfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
218 end
219
220 if ~isempty(idandmsg)
221 if constrflag
222 error(nonlconid,nonlconmsg);
223 else
224 error(idandmsg);
225 end
226 end
227 calltype = 'fun_then_grad_then_hess';
228 if ~hessflag && ~gradflag
229 warning('optimlib:optimfcnchk:HessianAndGradientOptionsOff', ...
230 ['Hessian and gradient functions provided but OPTIONS.Hessian=''off'' and ', ...
231 'OPTIONS.GradObj=''off''; ignoring Hessian and gradient functions. Rerun with ', ...
232 'OPTIONS.Hessian=''on'' and OPTIONS.GradObj=''on'' to use derivative functions.'])
233 calltype = 'fun';
234 elseif hessflag && ~gradflag
235 warning('optimlib:optimfcnchk:GradientOptionOff', ...
236 ['Hessian and gradient functions provided but OPTIONS.GradObj=''off''; ', ...
237 'ignoring Hessian and gradient functions. Rerun with OPTIONS.Hessian=''on'' ', ...
238 'and OPTIONS.GradObj=''on'' to use derivative functions.'])
239 calltype = 'fun';
240 elseif ~hessflag && gradflag
241 warning('optimlib:optimfcnchk:HessianOptionOff', ...
242 ['Hessian function provided but OPTIONS.Hessian=''off''; ignoring ', ...
243 'Hessian function. Rerun with OPTIONS.Hessian=''on'' to use Hessian function.']);
244 calltype = 'fun_then_grad';
245 end
246
247
0.01 860 248 elseif isa(funstr, 'cell') && length(funstr)==3 ...
249 && ~isempty(funstr{2}) && isempty(funstr{3}) % {fun, grad, []}
250 if hessflag
251 error(message('optimlib:optimfcnchk:NoHessianFunction'))
252 end
253 [funfcn, idandmsg] = fcnchk(funstr{1},lenVarIn);
254 if funValCheck
255 userfcn = funfcn;
256 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
257 end
258 if ~isempty(idandmsg)
259 if constrflag
260 error(nonlconid,nonlconmsg);
261 else
262 error(idandmsg);
263 end
264 end
265 [gradfcn, idandmsg] = fcnchk(funstr{2},lenVarIn);
266 if funValCheck
267 userfcn = gradfcn;
268 gradfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
269 end
270 if ~isempty(idandmsg)
271 if constrflag
272 error(nonlconid,nonlconmsg);
273 else
274 error(idandmsg);
275 end
276 end
277 calltype = 'fun_then_grad';
278 if ~gradflag
279 warning(warnid,warnstr);
280 calltype = 'fun';
281 end
282
283
860 284 elseif isa(funstr, 'cell') && length(funstr)==3 ...
285 && isempty(funstr{2}) && ~isempty(funstr{3}) % {fun, [], hess}
286 error(message('optimlib:optimfcnchk:NoGradientWithHessian'))
287
0.01 860 288 elseif ~isa(funstr, 'cell') %Not a cell; is a string expression, function name string or inline object
0.15 860 289 [funfcn, idandmsg] = fcnchk(funstr,lenVarIn);
860 290 if funValCheck
291 userfcn = funfcn;
292 funfcn = @checkfun; %caller and userfcn are in scope in nested checkfun
293 end
294
0.02 860 295 if ~isempty(idandmsg)
296 if constrflag
297 error(nonlconid,nonlconmsg);
298 else
299 error(idandmsg);
300 end
301 end
860 302 if gradflag % gradient and function in one function/MATLAB file
303 gradfcn = funfcn; % Do this so graderr will print the correct name
304 end
0.01 860 305 if hessflag && ~gradflag
306 warning('optimlib:optimfcnchk:GradientOptionOff', ...
307 ['OPTIONS.Hessian=''on'' but OPTIONS.GradObj=''off''; ignoring Hessian ', ...
308 'and gradient functions. Rerun with OPTIONS.Hessian=''on'' and ', ...
309 'OPTIONS.GradObj=''on'' to use derivative functions.']);
310 end
311
312 else
313 error('optimlib:optimfcnchk:MustBeAFunction', ...
314 ['FUN must be a function or an inline object; or, FUN ', ...
315 'may be a cell array that contains these type of objects.']);
316 end
317
0.01 860 318 allfcns{1} = calltype;
0.01 860 319 allfcns{2} = caller;
860 320 allfcns{3} = funfcn;
860 321 allfcns{4} = gradfcn;
860 322 allfcns{5} = hessfcn;
323
324 %------------------------------------------------------------
325 function [varargout] = checkfun(x,varargin)
326 % CHECKFUN checks for complex, Inf, or NaN results from userfcn.
327 % Inputs CALLER, USERFCN, and NTHETA come from the scope of OPTIMFCNCHK.
328 % We do not make assumptions about f, g, or H. For generality, assume
329 % they can all be matrices.
330
331 if nargout == 1
332 f = userfcn(x,varargin{:});
333 if any(any(isnan(f)))
334 error(message('optimlib:optimfcnchk:checkfun:NaNFval', functiontostring( userfcn ), upper( caller )));
335 elseif ~isreal(f)
336 error(message('optimlib:optimfcnchk:checkfun:ComplexFval', functiontostring( userfcn ), upper( caller )));
337 elseif any(any(isinf(f)))
338 error(message('optimlib:optimfcnchk:checkfun:InfFval', functiontostring( userfcn ), upper( caller )));
339 else
340 varargout{1} = f;
341 end
342
343 elseif nargout == 2 % Two output could be f,g (from objective fcn) or c,ceq (from NONLCON)
344 [f,g] = userfcn(x,varargin{:});
345 if any(any(isnan(f))) || any(any(isnan(g)))
346 error(message('optimlib:optimfcnchk:checkfun:NaNFval', functiontostring( userfcn ), upper( caller )));
347 elseif ~isreal(f) || ~isreal(g)
348 error(message('optimlib:optimfcnchk:checkfun:ComplexFval', functiontostring( userfcn ), upper( caller )));
349 elseif any(any(isinf(f))) || any(any(isinf(g)))
350 error(message('optimlib:optimfcnchk:checkfun:InfFval', functiontostring( userfcn ), upper( caller )));
351 else
352 varargout{1} = f;
353 varargout{2} = g;
354 end
355
356 elseif nargout == 3 % This case only happens for objective functions
357 [f,g,H] = userfcn(x,varargin{:});
358 if any(any(isnan(f))) || any(any(isnan(g))) || any(any(isnan(H)))
359 error(message('optimlib:optimfcnchk:checkfun:NaNFval', functiontostring( userfcn ), upper( caller )));
360 elseif ~isreal(f) || ~isreal(g) || ~isreal(H)
361 error(message('optimlib:optimfcnchk:checkfun:ComplexFval', functiontostring( userfcn ), upper( caller )));
362 elseif any(any(isinf(f))) || any(any(isinf(g))) || any(any(isinf(H)))
363 error(message('optimlib:optimfcnchk:checkfun:InfFval', functiontostring( userfcn ), upper( caller )));
364 else
365 varargout{1} = f;
366 varargout{2} = g;
367 varargout{3} = H;
368 end
369 elseif nargout == 4 & ~isequal(caller,'fseminf')
370 % In this case we are calling NONLCON, e.g. for FMINCON, and
371 % the outputs are [c,ceq,gc,gceq]
372 [c,ceq,gc,gceq] = userfcn(x,varargin{:});
373 if any(any(isnan(c))) || any(any(isnan(ceq))) || any(any(isnan(gc))) || any(any(isnan(gceq)))
374 error(message('optimlib:optimfcnchk:checkfun:NaNFval', functiontostring( userfcn ), upper( caller )));
375 elseif ~isreal(c) || ~isreal(ceq) || ~isreal(gc) || ~isreal(gceq)
376 error(message('optimlib:optimfcnchk:checkfun:ComplexFval', functiontostring( userfcn ), upper( caller )));
377 elseif any(any(isinf(c))) || any(any(isinf(ceq))) || any(any(isinf(gc))) || any(any(isinf(gceq)))
378 error(message('optimlib:optimfcnchk:checkfun:InfFval', functiontostring( userfcn ), upper( caller )));
379 else
380 varargout{1} = c;
381 varargout{2} = ceq;
382 varargout{3} = gc;
383 varargout{4} = gceq;
384 end
385 else % fseminf constraints have a variable number of outputs, but at
386 % least 4: see semicon.m
387 % Also, don't check 's' for NaN as NaN is a valid value
388 T = cell(1,ntheta);
389 [c,ceq,T{:},s] = userfcn(x,varargin{:});
390 nanfound = any(any(isnan(c))) || any(any(isnan(ceq)));
391 complexfound = ~isreal(c) || ~isreal(ceq) || ~isreal(s);
392 inffound = any(any(isinf(c))) || any(any(isinf(ceq))) || any(any(isinf(s)));
393 for ii=1:length(T) % Elements of T are matrices
394 if nanfound || complexfound || inffound
395 break
396 end
397 nanfound = any(any(isnan(T{ii})));
398 complexfound = ~isreal(T{ii});
399 inffound = any(any(isinf(T{ii})));
400 end
401 if nanfound
402 error(message('optimlib:optimfcnchk:checkfun:NaNFval', functiontostring( userfcn ), upper( caller )));
403 elseif complexfound
404 error(message('optimlib:optimfcnchk:checkfun:ComplexFval', functiontostring( userfcn ), upper( caller )));
405 elseif inffound
406 error(message('optimlib:optimfcnchk:checkfun:InfFval', functiontostring( userfcn ), upper( caller )));
407 else
408 varargout{1} = c;
409 varargout{2} = ceq;
410 varargout(3:ntheta+2) = T;
411 varargout{ntheta+3} = s;
412 end
413 end
414
415 end %checkfun
416 %----------------------------------------------------------
860 417 end % optimfcnchk