I'm trying to optimize the objective function
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
The optimization is using fmincon and is shown below
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));
The function for the nonlinear constraint is:
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
The error I'm getting is as below:
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));
What is the problem here?
Best Answer