1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## test_fminunc_compat_1 - Test that fminunc_compat and optimset_compat work
18 ## A quadratic function is fminunc_compatd. Various options are tested. Options
19 ## are passed incomplete (to see if properly completed) and
22 ok = 1; # Remains set if all ok. Set to 0 otherwise
23 cnt = 0; # Test counter
25 page_screen_output (0);
26 page_output_immediately (1);
28 if ! exist ("verbose"), verbose = 0; end
36 function v = ff(x,y,t)
37 A = [1 -1;1 1]; M = A'*diag([100,1])*A;
38 v = ((x - y)(1:2))'*M*((x-y)(1:2)) + 1;
42 ## Return value, diff and 2nd diff
43 function [v,dv,d2v] = d2ff(x,y,t)
44 if nargin < 3, t = 1; end
45 if t == 1, N = length (x); else N = length (y); end
46 A = [1 -1;1 1]; M = A'*diag([100,1])*A;
47 v = ((x - y)(1:2))'*M*((x-y)(1:2)) + 1;
48 dv = 2*((x-y)(1:2))'*M;
49 d2v = zeros (N); d2v(1:2,1:2) = 2*M;
50 if N>2, dv = [dv, zeros(1,N-2)]; end
51 if t == 2, dv = -dv; end
56 function prn (varargin), printf (varargin{:}); fflush (stdout); end
60 prn ("\n Testing that fminunc_compat() works as it should\n\n");
61 prn (" Nparams = N = %i\n",N);
65 ## Plain run, just to make sure ######################################
66 ## Minimum wrt 'x' is y0
67 opt = optimset_compat ();
68 [xlev,vlev] = fminunc_compat ("ff",x0,opt,y0,1);
71 if max (abs (xlev-y0)) > 100*sqrt (eps)
73 prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
76 elseif verbose, prn ("ok %i\n",cnt);
79 ## See what 'backend' gives in that last case ########################
80 opt = optimset_compat ("backend","on");
81 [method,ctl] = fminunc_compat ("ff",x0, opt, y0,1);
84 if ! ischar (method) || ! strcmp (method,"nelder_mead_min")
87 prn ("Wrong method '%s' != 'nelder_mead_min' was chosen\n", method);
89 prn ("fminunc_compat pretends to use a method that isn't a string\n");
94 elseif verbose, prn ("ok %i\n",cnt);
97 [xle2,vle2,nle2] = feval (method, "ff",{x0,y0,1}, ctl);
99 # nelder_mead_min is not very repeatable
100 # because of restarts from random positions
101 if max (abs (xlev-xle2)) > 100*sqrt (eps)
103 prn ("Error is too big : %8.3g\n", max (abs (xlev-xle2)));
106 elseif verbose, prn ("ok %i\n",cnt);
110 ## Run, w/ differential returned by function ('jac' option) ##########
111 ## Minimum wrt 'x' is y0
113 opt = optimset_compat ("GradO","on");
114 [xlev,vlev,nlev] = fminunc_compat ("d2ff",x0,opt,y0,1);
117 if max (abs (xlev-y0)) > 100*sqrt (eps)
119 prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
122 elseif verbose, prn ("ok %i\n",cnt);
126 ## Use the 'hess' option, when f can return 2nd differential #########
127 ## Minimum wrt 'x' is y0
128 opt = optimset_compat ("hessian","on");
129 [xlev,vlev,nlev] = fminunc_compat ("d2ff",x0,opt,y0,1);
132 if max (abs (xlev-y0)) > 100*sqrt (eps)
134 prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
137 elseif verbose, prn ("ok %i\n",cnt);
142 prn ( "All tests ok\n");