]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_d2_min_3.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_d2_min_3.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## Test whether d2_min() functions correctly
17 ##
18 ## Gives a 2-dim function with strange shape ("ff", defined below).
19 ##
20 ## Sets a ok variable to 1 in case of success, 0 in case of failure
21 ##
22 ## If a variables "verbose" is set, then some comments are output.
23
24 1 ;
25
26 ok = 0;
27
28 if ! exist ("verbose"), verbose = 0; end
29
30 if verbose
31   printf ("\n   Testing d2_min () on a strange 2-dimensional function\n\n");
32 end
33
34 P = 2;  # Nparams
35 noise = 0 ;
36 truep = [0;0] ;
37 xinit = randn(P,1) ;
38
39 if noise, obses = adnois(obses,noise); end
40
41 y = nan;
42
43
44 function v = ff (x, y)
45   v = x(1)^2 * (1+sin(x(2)*3*pi)^2) + x(2)^2;
46 endfunction
47
48
49 function [w,dv,d2v] = d2ff (x, y)
50   u = x(1); v = x(2);
51   w = u^2 * (1+sin(v*3*pi)^2) + v^2;
52
53   dv = [2*u * (1+sin(v*3*pi)^2), u^2 * sin(v*2*3*pi) + 2*v ];
54
55   d2v = [2*(1+sin(v*3*pi)^2), 2*u * sin(v*2*3*pi) ;
56          2*u * sin(v*2*3*pi), u^2 * 2*3*pi* cos(v*2*3*pi) + 2 ];
57   d2v = inv (d2v);
58 endfunction
59
60 ##       dt = mytic()
61 ##
62 ## Returns the cputime since last call to 'mytic'.
63
64 function dt = mytic()
65    persistent last_mytic = 0 ;
66    [t,u,s] = cputime() ;
67    dt = t - last_mytic ;
68    last_mytic = t ;
69 endfunction
70
71
72 ctl = nan*zeros(1,5); ctl(5) = 1;
73
74 if verbose
75   printf ( "Going to call d2_min\n");
76 end
77 mytic() ;
78 [xlev,vlev,nev] = d2_min ("ff", "d2ff", {xinit,y},ctl) ;
79 tlev = mytic ();
80
81 if verbose,
82   printf("d2_min should find minv = 0 (plus a little error)\n");
83   printf(["d2_min : niter=%-4d  nev=%-4d  nparams=%-4d\n",...
84           "  time=%-8.3g errx=%-8.3g   minv=%-8.3g\n"],...
85          nev([2,1]), P, tlev, max (abs (xlev-truep)), vlev);
86 end
87
88 ok = 1;
89
90 if max (abs(xlev-truep )) > sqrt (eps),
91   if verbose
92       printf ( "Error is too big : %-8.3g\n", max (abs (xlev-truep)));
93   end
94   ok = 0;
95 end
96
97 if verbose && ok
98     printf ( "All tests ok\n");
99 end
100
101
102