matlab调用nlopt时向目标函数中传入数据的案例,如代码所示:
clc,clear,close all
opt.algorithm = NLOPT_LN_AUGLAG;
opt.lower_bounds = -10;
opt.upper_bounds = 10;
opt.min_objective = @(x) goal_function(x,[1,2,3,4,5,6,7,8,9]);
opt.xtol_rel = 1e-8;
[xopt, fmin, retcode] = nlopt_optimize(opt, -3)
function [val, gradient] = goal_function(x,a)
a
val = 0.5*(x-1)^2+0.5;
if (nargout > 1)
gradient = 0;
end
end
可见a值能正常打印,数据传入成功。
Nlopt在matlab中的配置教程,参见传送门。