Too many outputs for fmincon problem

I have the following optimization problem
coder.extrinsic('ParameterSarah')
ParameterSarah;
coder.extrinsic('objective')
@objective;
coder.extrinsic('nlcon')
@nlcon;
Mz(i) = -w_vl*(cos(d(1))*FxW(1)-sin(d(1))*alpha(1)*c_w)+w_vr*(cos(d(2))*FxW(2)-sin(d(2))*alpha(2)*c_w)-w_hl*(cos(d(3))*FxW(3)-sin(d(3))*alpha(3)*c_w)+w_hr*(cos(d(4))*FxW(4)-sin(d(4))*alpha(4)*c_w)+l_v*(sin(d(1))*FxW(1)-cos(d(1))*alpha(1)*c_w+sin(d(2))*FxW(2)-cos(d(2))*alpha(2)*c_w)-l_h*(sin(d(3))*FxW(3)-cos(d(3))*alpha(3)*c_w+sin(d(4))*FxW(4)*c_w-cos(d(4))*alpha(4)*c_w);
Fy(i) = sin(d(i)).*FxW(i)-cos(d(i)).*alpha(i)*c_w;
Fx(i) = (FxW(i)/cos(d(i)))-c_w*sin(d(i)).*alpha(i);
%definition of x
x = (1:8);
x = [FxW(1); FxW(2); FxW(3); FxW(4); alpha(1); alpha(2); alpha(3); alpha(4)];
alpha(i) = d(i)- SW(i);
d(i) = [20; 20; 20; 20];
%initial guess
x0 = [0, 0, 0, 0, 0, 0, 0, 0];
%variable bounds
lb = zeros(8,1);
i = 1:4;
%case decision braking or driving backwards (decision input: flag=0 oder 1)
%braking mode
if flag > 0 %if it is possible the regenerative braking torque is the limit
if (Fx_v-sum(Fx(i),(1:4)) == 0) && (Fy_v-sum(Fy(i),(1:4)) == 0) && (Mz_v-sum(Mz(i),(1:4)) == 0) %if it is not possible to follow the target trajectory the maximum of the manual braking is used
% max. regenerative braking torque
lb(1:4) = [-M_reg(1)/r_dyn(1), -M_reg(2)/r_dyn(2), -M_reg(3)/r_dyn(3), -M_reg(4)/r_dyn(4)]; %regenerative braking torque must be positive
% mechanical braking
else
lb(1:4) = [-FxB_max, -FxB_max, -FxB_max, -FxB_max]; %FxB_max is negative
end % close conditions for braking loop, max braking torque is positive
%driving mode
else
lb(1:4) = [-P_max(1/(r_dyn(1)*w(1))), -P_max(1)/(r_dyn(2)*w(2)), -P_max(1)/(r_dyn(3)*w(3)), -P_max(1)/(r_dyn(4)*w(4))];
end
lb(5:8)= [d_min_vl-beta(:,1), d_min_vr-beta(:,2), d_min_hl-beta(:,3), d_min_hr-beta(:,4)]; %d and betato the left side are positive, to the right side are negative
ub(1:8) = [P_max(1)/(r_dyn(1)*w(1)), P_max(2)/(r_dyn(2)*w(2)), P_max(3)/(r_dyn(3)*w(3)), P_max(4)/(r_dyn(4)*w(4)), d_max_vl-beta(1), d_max_vr-beta(2), d_max_hl-beta(3), d_max_hr-beta(4)];
%linear constraints
A = [];
b = [];
%Definition alpha
Aeq = [6,8];
beq = (1:6);
Aeq = [0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0];
beq(1:6) = [d(1)-beta(1); d(2)-beta(2); d(3)-beta(3); d(4)-beta(4); 0; 0];
% active stimulation for beta
%if k = 1 there is a stimulation
% k, j and n are Inputs, j and n are the actors stimulated
if k == 1
(Aeq(5,j) == 1) && (Aeq(5,n) == -1) && (beq(5) == delta_TaA); % target moduls % delta torque
end
%steering priority
SOC_dif = (1:4);
SOC_dif(i)=(SOC(i)-(sum(SOC,i(1:4))/4));
if (SOC_dif(1) < SOC_crit) && (flag == 1) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(1,p) = 1;
end
if (SOC_dif(2) < SOC_crit) && (flag == 1) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(2,p) = 1;
end
if (SOC_dif(3) < SOC_crit) && (flag == 1) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(3,p) = 1;
end
if (SOC_dif(4) < SOC_crit) && (flag == 1) % state of charge of one actor is really low, actor is only used for steering %during driving mode
Aeq(4,p) = 1;
end
%optimize with fmincon
x = fmincon(@(x)objective,x0,A,b,Aeq,beq,lb,ub,@nlcon);
with the nonlinear Constraints
function nlcon
coder.extrinsic('ParameterSarah')
ParameterSarah;
i = 1:4;
alpha(i) = d(i)- SW(i);
d(i) = [20; 20; 20; 20];
i = 1:4;
%Following the target trajectory ( equal for braking or driving mode)
Ceq = (1:3);
Mz(i) = -w_vl*(cos(d(1))*FxW(1)-sin(d(1))*alpha(1)*c_w)+w_vr*(cos(d(2))*FxW(2)-sin(d(2))*alpha(2)*c_w)-w_hl*(cos(d(3))*FxW(3)-sin(d(3))*alpha(3)*c_w)+w_hr*(cos(d(4))*FxW(4)-sin(d(4))*alpha(4)*c_w)+l_v*(sin(d(1))*FxW(1)-cos(d(1))*alpha(1)*c_w+sin(d(2))*FxW(2)-cos(d(2))*alpha(2)*c_w)-l_h*(sin(d(3))*FxW(3)-cos(d(3))*alpha(3)*c_w+sin(d(4))*FxW(4)*c_w-cos(d(4))*alpha(4)*c_w);
Fy(i) = sin(d(i)).*FxW(i)-cos(d(i)).*alpha(i)*c_w;
Fx(i) = (FxW(i)/cos(d(i)))-c_w*sin(d(i)).*alpha(i);
Ceq(1) = Fy_v-sum(Fy(i),(1:4));
Ceq(2) = Mz_v-sum(Mz(i),(1:4));
Ceq(3) = Fx_v-sum(Fx(i),(1:4));
C = [];
disp(Ceq(3));
end
and the objective function
function objective
coder.extrinsic('ParameterSarah')
ParameterSarah;
i = 1:4;
S_1 = (1:4);
S_2 = (1:4);
%case decision
if f_lag > 0 % decision braking or driving mode
S_1(1) = (A/SOC(1))*abs(FxW(1)); %driving mode
S_1(2) = (A/SOC(2))*abs(FxW(2));
S_1(3) = (A/SOC(3))*abs(FxW(3));
S_1(4) = (A/SOC(4))*abs(FxW(4));
else
S_1(1) = ((B*M_reg(1))/SOC(1))*(1/FxW(1)); %braking mode
S_1(2) = ((B*M_reg(2))/SOC(2))*(1/FxW(2));
S_1(3) = ((B*M_reg(3))/SOC(3))*(1/FxW(3));
S_1(4) = ((B*M_reg(4))/SOC(4))*(1/FxW(4));
end
%delta steering angle
delta_d(i) = d_delay(i)-(SW(i)+alpha(i)); %d_delay in a seperate file or in simulink, equals d(t-1)
%minimizing the steering movement
S_2(i) = C*delta_d(i);
% sum of the three objectives
z_1 = sum(S_1,(1:4));
z_2 = sum(S_2,(1:4));
%objektive function
objective(FxW(1), FxW(2), FxW(3), FxW(4), alpha(1), alpha(2), alpha(3), alpha(4)) = z_1 + z_2;
end
They all run seperately now but during the Opimization I get this error
Error using objective
Too many output arguments.
Error in Optimierungsproblem>@(x)objective (line 106)
x = fmincon(@(x)objective,x0,A,b,Aeq,beq,lb,ub,@nlcon);
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in Optimierungsproblem (line 106)
x = fmincon(@(x)objective,x0,A,b,Aeq,beq,lb,ub,@nlcon);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
Can someone help me? I have no idea whats the problem.

Answers (0)

Asked:

on 15 Jan 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!