[Scilab-users] Optim & NelderMead use [closed]
Carrico, Paul
paul.carrico at esterline.com
Mon Jan 16 10:30:20 CET 2017
Hi all
After performing tests (and modifying the target function as it should have been done first), I can better understand how to use 'optim' and 'Neldermead' procedures.
For my needs the mean flags are :
- Step h in numderivative --> usefull reading as "EE 221 Numerical Computing" Scott Hudson
- The threshold epsg in optim (%eps is the default value - such high accuracy is not necessary for my application - furthermore using a value such as 1e-5 leads to err=1 that is correct for checking)
- Ditto for Nelder-Mead and '-tolfunrelative' & '-tolxrelative'
Now it works fine :-)
Thanks all for the support
Paul
#####################################################################
mode(0)
clear
global count_use;
count_use = 0;
// ****
function f=lineaire(x, a2, b2)
f = a2*x+b2;
endfunction
// ****
function g=racine(x, a1, b1)
g = sqrt(a1*x) + b1;
endfunction
// ****
function f=target(x, a1, b1, a2, b2)
val_lin = lineaire(x,a2,b2);
val_rac = racine(x,a1,b1);
f = sqrt((val_lin - val_rac)^2);
global count_use;
count_use = count_use +1;
endfunction
// Cost function:
function [f, g, ind]=cost(x, ind, a1, b1, a2, b2)
f = target(x);
// g = numderivative(target, x.',order = 4);
g = numderivative(target, x.',1e-3, order = 4); // 1E-3 => see EE 221 "Numerical Computing" Scott Hudson
// Study of the influence of h on the number of target function calculation & the fopt accuracy:
// (epsg = %eps here)
// h = 1.e-1 => number = 220 & fopt = 2.242026e-05
// h = 1.e-2 => number = 195 & fopt = 2.267564e-07
// h = 1.e-3 => number = 170 & fopt = 2.189495e-09 ++++++
// h = 1.e-4 => number = 190 & fopt = 1.941203e-11
// h = 1.e-5 => number = 215 & fopt = 2.131628e-13
// h = 1.e-6 => number = 235 & fopt = 0.
// h = 1.e-7 => number = 255 & fopt = 7.105427e-15
// h = 1.e-8 => number = 275 & fopt = 0.
endfunction
// *************************
// optimisation with optim
initial_parameters = [10]
lower_bounds = [0];
upper_bounds = [1000];
nocf = 1000; // number max of call of f
niter = 1000; // number max of iterations
a1 = 30;
b1 = 2.5;
a2 = 1;
b2 = 2;
epsg = 1e-5; // gradient norm threshold (%eps by defaut) --> lead to err = 1 !!!
//epsg = %eps; // lead to Err = 13
epsf = 0; //threshold controlling decreasing of f (epsf = 0 by defaut)
costf = list (cost, a1, b1, a2, b2);
[fopt, xopt, gopt, work, iters, evals, err] = optim(costf,'b',lower_bounds,upper_bounds,initial_parameters,'qn','ar',nocf,niter,epsg,epsf);
printf("Optimized value : %g\n",xopt);
printf("min cost function value (should be as closed as possible to 0) ; %e\n",fopt);
printf('Number of calculations = %d !!!\n',count_use);
// Curves definition
x = linspace(0,50,1000)';
plot_raci = racine(x,a1,b1);
plot_lin = lineaire(x,a2,b2);
scf(1);
drawlater();
xgrid(3);
f = gcf();
//f
f.figure_size = [1000, 1000];
f.background = color(255,255,255);
a = gca();
//a
a.font_size = 2;
a.x_label.text = "X axis" ;
a.x_location="bottom";
a.x_label.font_angle=0;
a.x_label.font_size = 4;
a.y_label.text = "Y axis";
a.y_location="left";
a.y_label.font_angle=-90;
a.Y_label.font_size = 4;
a.title.text = "Title";
a.title.font_size = 5;
a.line_style = 1;
// Curves plot
plot(x,plot_lin);
e1 = gce();
p1 = e1.children;
p1.thickness = 1;
p1.line_style = 1;
p1.foreground = 3;
plot(x,plot_raci);
e2 = gce();
p2 = e2.children;
p2.thickness = 1;
p2.line_style = 1;
p2.foreground = 2;
drawnow();
EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170116/cadb5599/attachment.htm>
More information about the users
mailing list