主要内容

基于问题使用多起点求解器最小化非线性函数

从点 [–1,2] 开始,在范围 -5x,y5 上求 peaks 函数的局部最小值。

x = optimvar("x",LowerBound=-5,UpperBound=5);
y = optimvar("y",LowerBound=-5,UpperBound=5);
x0.x = -1;
x0.y = 2;
prob = optimproblem(Objective=peaks(x,y));
opts = optimoptions("fmincon",Display="none");
[sol,fval] = solve(prob,x0,Options=opts)
sol = struct with fields:
    x: -3.3867
    y: 3.6341

fval = 
1.1224e-07

尝试使用 GlobalSearch 求解器求得更好的解。此求解器多次运行 fmincon,可能会产生更好的解。

ms = GlobalSearch;
[sol2,fval2] = solve(prob,x0,ms)
Solving problem using GlobalSearch.

GlobalSearch stopped because it analyzed all the trial points.

All 15 local solver runs converged with a positive local solver exit flag.
sol2 = struct with fields:
    x: 0.2283
    y: -1.6255

fval2 = 
-6.5511

GlobalSearch 找到一个目标函数值更好(更低)的解。退出消息显示局部求解器 fmincon 运行了 15 次。返回的解的目标函数值约为 –6.5511,低于第一个解的值 1.1224e–07。

另请参阅

| | |

主题