Deleted vectorized computation feature. Deleted neldermead_contour. Fixed the demos.
[scilab.git] / scilab / modules / optimization / demos / neldermead / neldermead_rosenbrock.sce
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 mprintf("Defining Rosenbrock function...\n");
11 function y = rosenbrock (x)
12 y = 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
13 endfunction
14 x0 = [-1.2 1.0]';
15 mprintf("x0=%s\n",strcat(string(x0)," "));
16 mprintf("Creating object...\n");
17 nm = neldermead_new ();
18 mprintf("Configuring object...\n");
19 nm = neldermead_configure(nm,"-numberofvariables",2);
20 nm = neldermead_configure(nm,"-function",rosenbrock);
21 nm = neldermead_configure(nm,"-x0",x0);
22 nm = neldermead_configure(nm,"-maxiter",200);
23 nm = neldermead_configure(nm,"-maxfunevals",300);
24 nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
25 nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
26 nm = neldermead_configure(nm,"-simplex0method","axes");
27 nm = neldermead_configure(nm,"-simplex0length",1.0);
28 nm = neldermead_configure(nm,"-method","variable");
29 nm = neldermead_configure(nm,"-verbose",0);
30 nm = neldermead_configure(nm,"-verbosetermination",0);
31 mprintf("Searching for minimum...\n");
32 nm = neldermead_search(nm);
33 neldermead_display(nm);
34 mprintf("Plot contour...\n");
35 xmin = -2.0 ; xmax = 2.0 ; ymin = -2.0 ; ymax = 2.0 ; nx = 100 ; ny = 100;
36 stepx = (xmax - xmin)/nx
37 xdata = xmin:stepx:xmax;
38 stepy = (ymax - ymin)/ny
39 ydata = ymin:stepy:ymax;
40 for ix = 1:length(xdata)
41   for iy = 1:length(ydata)
42     experiment = [xdata(ix) ydata(iy)];
43     [ nm , fiexp ] = neldermead_function ( nm , experiment );
44     zdata ( ix , iy ) = fiexp;
45   end
46 end
47 wnum = 100001;
48 my_handle             = scf(wnum);
49 contour ( xdata , ydata , zdata , [1 10 100 500 1000 2000] )
50 // Plot starting point
51 mprintf("x0 : blue dot\n");
52 plot(x0(1),x0(2));
53 my_handle.children.children(1).children.mark_mode="on";
54 my_handle.children.children(1).children.mark_size = 5;
55 my_handle.children.children(1).children.mark_foreground = 2;
56 mprintf("xopt : red dot\n");
57 xopt = neldermead_get(nm,"-xopt");
58 plot(xopt(1),xopt(2));
59 my_handle.children.children(1).children.mark_mode="on";
60 my_handle.children.children(1).children.mark_size = 5;
61 my_handle.children.children(1).children.mark_foreground = 5;
62
63 nm = neldermead_destroy(nm);
64 clear nm;
65
66