annotate main/optim/inst/test_d2_min_3.m @ 9930:d30cfca46e8a octave-forge

optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
author carandraug
date Fri, 30 Mar 2012 15:14:48 +0000
parents 97f77d4bd426
children 28b882879c6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@cs.uky.edu>
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
2 ##
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
6 ## version.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
7 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
11 ## details.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
12 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 9389
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
15
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
16 ## Test whether d2_min() functions correctly
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
17 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
18 ## Gives a 2-dim function with strange shape ("ff", defined below).
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
19 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
20 ## Sets a ok variable to 1 in case of success, 0 in case of failure
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
21 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
22 ## If a variables "verbose" is set, then some comments are output.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
23
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
24 1 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
25
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
26 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
27
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
28 if ! exist ("verbose"), verbose = 0; end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
29
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
30 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
31 printf ("\n Testing d2_min () on a strange 2-dimensional function\n\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
32 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
33
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
34 P = 2; # Nparams
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
35 noise = 0 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
36 truep = [0;0] ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
37 xinit = randn(P,1) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
38
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
39 if noise, obses = adnois(obses,noise); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
40
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
41 y = nan;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
42
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
43
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
44 function v = ff (x, y)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
45 v = x(1)^2 * (1+sin(x(2)*3*pi)^2) + x(2)^2;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
46 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
47
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
48
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
49 function [w,dv,d2v] = d2ff (x, y)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
50 u = x(1); v = x(2);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
51 w = u^2 * (1+sin(v*3*pi)^2) + v^2;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
52
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
53 dv = [2*u * (1+sin(v*3*pi)^2), u^2 * sin(v*2*3*pi) + 2*v ];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
54
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
55 d2v = [2*(1+sin(v*3*pi)^2), 2*u * sin(v*2*3*pi) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
56 2*u * sin(v*2*3*pi), u^2 * 2*3*pi* cos(v*2*3*pi) + 2 ];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
57 d2v = inv (d2v);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
58 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
59
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
60 ## dt = mytic()
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
61 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
62 ## Returns the cputime since last call to 'mytic'.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
63
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
64 function dt = mytic()
9389
97f77d4bd426 replacing instances of `static' by `persistent' since it has been deprecated
carandraug
parents: 7140
diff changeset
65 persistent last_mytic = 0 ;
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
66 [t,u,s] = cputime() ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
67 dt = t - last_mytic ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
68 last_mytic = t ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
69 endfunction
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
70
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
71
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
72 ctl = nan*zeros(1,5); ctl(5) = 1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
73
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
74 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
75 printf ( "Going to call d2_min\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
76 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
77 mytic() ;
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 2370
diff changeset
78 [xlev,vlev,nev] = d2_min ("ff", "d2ff", {xinit,y},ctl) ;
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
79 tlev = mytic ();
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
80
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
81 if verbose,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
82 printf("d2_min should find minv = 0 (plus a little error)\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
83 printf(["d2_min : niter=%-4d nev=%-4d nparams=%-4d\n",...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
84 " time=%-8.3g errx=%-8.3g minv=%-8.3g\n"],...
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
85 nev([2,1]), P, tlev, max (abs (xlev-truep)), vlev);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
86 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
87
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
88 ok = 1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
89
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
90 if max (abs(xlev-truep )) > sqrt (eps),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
91 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
92 printf ( "Error is too big : %-8.3g\n", max (abs (xlev-truep)));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
93 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
94 ok = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
95 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
96
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
97 if verbose && ok
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
98 printf ( "All tests ok\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
99 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
100
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
101
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
102