问题
I have build a Simulink model for the dynamic and kinematic modeling of the vehicle, the vehicle has to track a reference trajectory, So the objective of the optimization is to minimize the error defined by (y) in "objec_Fun". And the constraint of the vehicle motion is the linear speed, angular speed, linear acceleration, and angular acceleration defined by (c) in "constraint_Fun". And the design parameter is the controller gains.
the vehicle linear and angular speeds must be reached to 8 m/s and -0.8 rad/s respectively as a steady-state.
Firstly; I run the code without nonlinear constraint function c=[], and it gives a perfect result to minimize the error (y) but the linear speed, angular speed, linear acceleration, and angular acceleration are not realistic.
Then I have put a limitation on the speeds and accelerations, but the fmincon can't minimize the errors.
the simulation time is 10 sec, and the symbols of the speeds and accelerations are "VV" and "VVdot". "VV" is 10001*3 matrix the first column is the time, the second is linear speed, and the third is the angular speed.
"VVdot" is 10001*3 matrix the first column is the time, the second is linear acceleration, and the third is the angular acceleration.
Main Matlab File
global K1 K2 Roh delta1 delta2 kx ky ktheta VV VVdot
K1=1; K2=1; Roh=1; delta1=0.00001; delta2=0.00001; kx=1; ky=1; ktheta=1;
lb=[0.00 0.00 0.00 0.00001 0.00001 0.00 0.00 0.00 ];
ub=[0200 0200 0200 0.1 0.1 0200 0200 0200 ];
x0=[K1 K2 Roh delta1 delta2 kx ky ktheta];
A=[]; b=[]; Aeq=[]; beq=[];
options = optimset( 'Algorithm','sqp','Display','iter','PlotFcn','optimplotfval','TolFun',10e-6,'TolCon',10e-6);
[x,fval,exitflag,output,grad]=fmincon(@objec_Fun,x0,A,b,Aeq,beq,lb,ub,@constraint_Fun,options)
objec_Fun
function y=objec_Fun(x)
global K1 K2 Roh delta1 delta2 kx ky ktheta VV VVdot
K1=x(1);
K2=x(2);
Roh=x(3);
delta1=x(4);
delta2=x(5);
kx=x(6);
ky=x(7);
ktheta=x(8);
%%
options=simset('SrcWorkspace','current');
sim('DynSMC23092019.slx',[],options);
assignin('base','x',x)
assignin('base','VV',VV)
assignin('base','VVdot',VVdot)
%%
errx=ep.Data(:,1);
erry=ep.Data(:,2);
errtheta=ep.Data(:,3);
errd=sqrt(errx.^2+erry.^2);
N=length(errtheta);
y=(1/N)*(sum(errd.^2+errtheta.^2));
end
constraint_Fun
function [c,ceq]=constraint_Fun(x)
global K1 K2 Roh delta1 delta2 kx ky ktheta VV VVdot
K1=x(1);
K2=x(2);
Roh=x(3);
delta1=x(4);
delta2=x(5);
kx=x(6);
ky=x(7);
ktheta=x(8);
c=[max(VV(:,2))-10; max(abs(VV(:,3)))-1.5 ;...
max(VVdot(:,2))-(10/3); max(abs(VVdot(:,3)))-(1.5/3)];
ceq=[];
end
来源:https://stackoverflow.com/questions/58252567/how-to-use-fmincon-to-optimize-the-objective-fun-with-constraint-variables-from