forked from stevengj/nlopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_octave.m
35 lines (29 loc) · 840 Bytes
/
t_octave.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
arg_list = argv ();
for i = 1:nargin
loadpath = arg_list{i};
printf ('-- adding path: %s\n', loadpath);
addpath (loadpath);
endfor
function [val, gradient] = myfunc(x)
val = sqrt(x(2));
if (nargout > 1)
gradient = [0, 0.5 / val];
end
endfunction
function [val, gradient] = myconstraint(x,a,b)
val = (a*x(1) + b)^3 - x(2);
if (nargout > 1)
gradient = [3*a*(a*x(1) + b)^2, -1];
end
endfunction
opt.algorithm = NLOPT_LD_MMA;
% opt.algorithm = NLOPT_LN_COBYLA
opt.lower_bounds = [-inf, 0];
opt.min_objective = @myfunc;
opt.fc = { (@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1)) };
opt.fc_tol = [1e-8, 1e-8];
opt.xtol_rel = 1e-4;
[xopt, fmin, retcode] = nlopt_optimize(opt, [1.234 5.678])
assert (retcode, 4);
assert (fmin, 0.5443, 1e-4);
assert (xopt, [0.3333, 0.2963], 1e-4);