求解非线性问题的函数:
操作步骤(最优):
1、先试用蒙特卡洛模拟优先求出最优的初始值X0
2、使用函数:fincon求解最优解
clc,clear
% 设置蒙特卡洛模拟的次数:
n = 10000000;
fmin = +inf;
x1 = unifrnd(-100,100,n,1);
x2 = unifrnd(-100,100,n,1);
for i = 1:n
x = [x1(i), x2(i)];
if ((x(1) - 1)^2 - x(2) <= 0) & (-2*x(1) + 3*x(2) - 6 <= 0)
res = -x(1)^2 -x(2)^2 + x(1)*x(2) + 2*x(1) + 5*x(2);
if(res < fmin)
fmin = res;
x0 = x;
end
end
end
disp("蒙特卡洛算法选取的初始值为:");
disp(x0);
A = [-2 3];
b = 6;
% 默认算法使用内点法
[x,fval] = fmincon(@fun1,x0,A,b,[],[],[],[],@nonfun1);
fval = -fval