麻烦在MATLAB中使用fmincon

时间:2017-10-25 19:25:16

标签: matlab optimization constraints

我正在为我的项目编写一个算法,在某些时候我需要找到alpha的值,它将使受约束的函数最小化。

  

错误:使用horzcat非标量函数句柄数组不是   允许;改为使用单元格数组。

这就是我所拥有的

syms x1 x2 x3 x4;
syms alpha;
f=(exp(x1))*(4*(x1).^2 + 2*(x2).^2 + 4*(x1)*(x2) + 2*(x2) + 1) + 0*x3 + 0*x4;
%constraints must be equal to 0
g1=x1*x2-x1-x2+x3+1.5;
g2=x1*x2-x4+10;
%Give initial values, given that x1 is between -10 and -9 and x2 is between
%1 and 1.5, and the slacks x3 and x4 are strictly between 0 and 1.
lb=[-10 1 0 0];
ub=[-9 1.5 1 1];
xo=[-9 1 0.1 0.1];



if (-10<=xo(1)) && (xo(1)<=-9) && (1<=xo(2)) && (xo(2)<=1.5)
    xi(1)=xo(1);
    xi(2)=xo(2);
    xd(1)=xo(3);
    xd(2)=xo(4);
end



    %Evaluation of Gradient of f
    gradfi(1,1)=subs(diff(f,x1),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    gradfi(2,1)=subs(diff(f,x2),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});

    gradfd(1,1)=subs(diff(f,x3),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    gradfd(2,1)=subs(diff(f,x4),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    %Evaluation of Jacobian Matrix
    Ji1=subs(jacobian(g1,[x1 x2]),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    Ji2=subs(jacobian(g2,[x1 x2]),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    Ji=[Ji1;Ji2];
    Jd1=subs(jacobian(g1,[x3 x4]),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    Jd2=subs(jacobian(g2,[x3 x4]),{x1 x2 x3 x4},{xi(1) xi(2) xd(1) xd(2)});
    Jd=[Jd1;Jd2];

    gammai=-(gradfi - ((gradfd.')*Jd^(-1)*Ji).');
    deltad=-Jd^(-1)*Ji*(gammai);



x1=xi(1)+gammai(1)*alpha;
x2=xi(2)+gammai(2)*alpha;
x3=xd(1)+deltad(1)*alpha;
x4=xd(2)+deltad(1)*alpha;



falpha =matlabFunction((exp(x1))*(4*(x1).^2 + 2*(x2).^2 + 4*(x1)*(x2) + 2*(x2) + 1) + 0*x3 + 0*x4);
c=([-10-x1; x1+9; -x2+1;x2-1.5;-x3;x3-1;-x4;x4-1]);
ceq=([x1*x2-x1-x2+x3+1.5;x1*x2-x4+10]);
const=([matlabFunction(c), ceq]);
[alpha]=fmincon(falpha,0,[],[],[],[],[],[],const);

0 个答案:

没有答案