2

对于两个变量函数,例如f(x,y)=x^2+y+b,其中b是:

b=raylrnd(1*sqrt(2/pi),10^6,1)  %% b is 1x1000000 vector

并受到以下约束:2*x+1<=b

我想找到函数的最大值,比如 x 在 [-10,10] 之间,y 在 [-10,10] 之间(当然,我的实际函数比这更完整,我需要帮助设置框架,以便我可以将其应用于我的实际功能)。

有没有办法实现这个?


试图:

第 1 步:编写文件 objfun.m。

function f = objfun(x,b)
f = x(1)^2+(2)+b;

第 2 步:为非线性约束编写文件 confuneq.m。

function [c, ceq] = confuneq(x)
% Nonlinear inequality constraints
c = 2*x(1)+1-b;

第 3 步:调用约束优化例程。

for i=1:1:length(b)
bi=b(i);
x0 = [-1,1];            % Make a starting guess at the solution
options = optimoptions(@fmincon,'Algorithm','sqp');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],... 
   @confuneq,options);
4

0 回答 0