在MATLAB中使用fmincon时出错(屏障线402中的错误)

时间:2016-04-12 15:32:12

标签: matlab

我试图优化目标函数

function f = Objfun(u,x0)
[T,X] = ode45(@(t,x)eqs(t,x,u), [0 132], x0);
f = -X(end,2)*X(end,4);
end

优化使用fmincon,如下所示

clc; close all; clear ;
X1 = [1.5 0 0 7];
lb = 0;
ub = 50;
[x, fval] = fmincon(@(u)Objfun(u, X1), 11.25, [],[],[],[], lb,ub,@(u)nonlincon(u,X1));

非线性约束的函数是:

function [c,ceq] = nonlincon(u,x0)
[T,x] = ode45(@(t,x)eqs(t,x,u), [0 132], x0);
c = [];
c(:,1) = -x(:,1);
c(:,2) = x(:,1) - 10;
c(:,3) = -x(:,3);
c(:,4) = x(:,3) - 25;
c(:,5) = -x(:,4);
c(:,6) = x(:,4) - 10;
ceq = [];
end

我得到的错误如下:

 In an assignment  A(:) = B, the number of elements in A and B must be the same.
 Error in barrier (line 402)
Error in fmincon (line 818)
    [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in SolveUwoBound (line 6)
[x, fval] = fmincon(@(u)Objfun(u, X1), 11.25, [],[],[],[], lb,ub,@(u)nonlincon(u,X1));

这里有什么问题?

0 个答案:

没有答案