基于问题的有理目标函数
基于问题的优化方法涉及创建优化变量,并用这些变量表示目标和约束。
有理函数是多项式的商。当目标函数是优化变量的有理函数或其他支持的函数时,您可以直接从变量创建目标函数表达式。相反,当您的目标函数不是受支持的函数时,您必须创建一个表示目标的 MATLAB® 函数,然后使用 fcn2optimexpr
将该函数转换为表达式。请参阅优化变量和表达式支持的运算和将非线性函数转换为优化表达式。
例如,编写目标函数
(使用两个优化变量 x
和 y
。)
x = optimvar('x'); y = optimvar('y'); f = (x-y)^2/(4+(x+y)^4)*(x+y^2)/(1+y^2);
为了找到此目标函数的最小值,创建一个以 f
为目标的优化问题,设置初始点,并调用 solve
。
prob = optimproblem('Objective',f);
x0.x = -1;
x0.y = 1;
[sol,fval,exitflag,output] = solve(prob,x0)
Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol = struct with fields:
x: -2.1423
y: 0.7937
fval = -1.0945
exitflag = OptimalSolution
output = struct with fields:
iterations: 9
funcCount: 10
stepsize: 1.7073e-06
lssteplength: 1
firstorderopt: 1.4999e-07
algorithm: 'quasi-newton'
message: 'Local minimum found....'
objectivederivative: "reverse-AD"
solver: 'fminunc'
退出标志表明报告的解是局部最小值。输出结构体显示求解器仅用 30 次函数计算就达到该最小值。