chiark / gitweb /
strip
[nlopt.git] / test / t_octave.m
1
2 arg_list = argv ();
3 for i = 1:nargin
4     loadpath = arg_list{i};
5     printf ('-- adding path: %s\n', loadpath);
6     addpath (loadpath);
7 endfor
8
9 function [val, gradient] = myfunc(x)
10     val = sqrt(x(2));
11     if (nargout > 1)
12         gradient = [0, 0.5 / val];
13     end
14 endfunction
15
16 function [val, gradient] = myconstraint(x,a,b)
17     val = (a*x(1) + b)^3 - x(2);
18     if (nargout > 1)
19         gradient = [3*a*(a*x(1) + b)^2, -1];
20     end
21 endfunction
22
23
24 opt.algorithm = NLOPT_LD_MMA
25 %  opt.algorithm = NLOPT_LN_COBYLA
26 opt.lower_bounds = [-inf, 0]
27 opt.min_objective = @myfunc
28 opt.fc = { (@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1)) }
29 opt.fc_tol = [1e-8, 1e-8];
30 opt.xtol_rel = 1e-4
31 [xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678])