MATLAB求下列式子的最优化问题

c=x-(d+sqrt(d^2-400^2))/2;
eq1=(2*x-d*cos(t)-d-sqrt(d^2-160000)).^2+(2*y-d*sin(t)-400).^2-d.^2;
eq2=(2*x-2*d*cos(t)).^2+(2*y-2*d*sin(t)).^2-(d-sqrt(d^2-160000)).^2-160000;
t=-20/180*pi:pi/1800:20/180*pi;
自己写的函数

考虑用非线性约束优化函数fmincon求解:

function zd

% x(1)=x,x(2)=d,x(3)=y,x(4)=t

objfun = @(x) x(1)-(x(2)+sqrt(x(2)^2-400^2))/2;
lb = [-inf -inf -inf -20];
ub = [inf inf inf 20];
x0 = [7100 7100 1800 15];
opt = optimset('MaxFunEvals',5e5);
[x,fval,exitflag] = fmincon(objfun,x0,[],[],[],[],lb,ub,@nlcon)

function [c,ce]=nlcon(x)
d=x(2);
y=x(3);
t=x(4)*pi/180;
x=x(1);
ce(1)=(2*x-d*cos(t)-d-sqrt(d^2-160000)).^2+(2*y-d*sin(t)-400).^2-d.^2;
ce(2)=(2*x-2*d*cos(t)).^2+(2*y-2*d*sin(t)).^2-(d-sqrt(d^2-160000)).^2-160000;
c = [];

 

其中的初值x0选择很重要,我是经过很多次尝试得到的。

程序输出:

Optimization terminated: directional derivative predicts change in
  objective value less than options.TolFun and maximum constraint violation
  is less than options.TolCon.
No active inequalities.

x =

  1.0e+003 *

    7.0641    7.1385    1.6589    0.0145


fval =

  -68.7183


exitflag =

     5

得到的优化结果是x=7064.1, d=7138.5, y=1658.9, theta=14.5。按照退出代码exitflag 看,该点很可能但并不能保证)是最优值。

追问

首先十分感谢你的回答,还有个问题就是优化的是D的值 在保证c=x-(d+sqrt(d^2-400^2))/2;最小的前提下 theta不能有定值吧

追答

现在优化的是x、d、y、theta四个变量,其中后两个变量未出现在目标函数中。
theta按照你的要求在-20~20度之间,如果你要求取定值,当然也可以修改其上下限,但是否有解要看具体取值。

温馨提示:答案为网友推荐,仅供参考
相似回答