您可以使用约束优化功能fmincon
:
% sample data
nvars = 1000;
A = rand(69,1);
B = rand(69,nvars);
% Objective function
fun_Obj = @(Alpha,A,B) norm(A- sum(Alpha.*B(:,:),2),2);
% Nonlinear constraint function. The first linear equality is set to -1 to make it alway true.
% The second induces the constraint that sum(Alpha) == 1
confuneq = @(Alpha)deal(-1, sum(Alpha)-1);
% starting values
x0 = ones(1,nvars)/nvars;
% lower and upper bounds
lb = zeros(1,nvars);
ub = ones(1,nvars);
% Finally apply constrained minimisation
Alpha = fmincon(@(x)fun_Obj(x,A,B),x0,[],[],[],[],lb,ub,@(x)confuneq(x));
在我的笔记本电脑上使用默认的迭代次数只需要几秒钟,但您应该考虑大幅增加该次数以获得更好的结果。此外,在这种情况下,默认算法可能不是正确的选择,'sqp'
可能更好。请参阅文档。
您可以通过以下方式执行这些操作:
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',1e6);
Alpha = fmincon(@(x)fun_Obj(x,A,B),x0,[],[],[],[],lb,ub,@(x)confuneq(x),options);