Objective function is undefined at initial point. Fmincon cannot continue.
13 ビュー (過去 30 日間)
古いコメントを表示
When executing the code below (in an 's-function'), I get the error message: "Objective function is undefined at initial point. Fmincon cannot continue."
Wearing, when I test the code contained in the "mdloutputs" function, which in this case contains the objective function, it works!!
I'd really like to know what's wrong and get some help.
Thank you !!
function [sys,x0,str,ts] = potential_field(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case { 2, 4, 9 },
sys = [];
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts]=mdlInitializeSizes(~,~,~,~)
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 5;
sizes.NumInputs = 6;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlOutputs(~,~,u,~,~,~,~)
a0=0;
av=100;
c=0;
b1=0;
b2=0;
yub=u(4).*sin(u(4))+10; %upper boundary
ylb=u(4).*sin(u(4))-10; %lower boundary
y=.5*(yub+ylb); %center line
Urep_1=zeros(1,1);
Urep_2=zeros(1,1);
if (ylb<=u(5))&&(yub>=u(5))
Urep_1=b1/(u(5)-yub)^2;
Urep_2=b2/(u(5)-ylb)^2;
end
J=@(X)(a0*(X(1)-u(1))^2 + av*(u(1)*tan(X(2))-u(6))^2 + Urep_1 + Urep_2 + c*(X(2)-u(3))^2);
X0=[1,15]; %Initial point for X
A = [];
b = [];
Aeq=[]; %Matrix for linear equality constraints
beq=[]; %Vector for linear equality constraints
lb=[0,-pi/2]; %Vector of lower bounds
ub=[90,pi/2]; %Vector of upper bounds
Q=fmincon(J,X0,A,b,Aeq,beq,lb,ub);
sys(1)= Q(1);
sys(2)= Q(2);
sys(3)= yub;
sys(4)= ylb;
sys(5)= y;
0 件のコメント
回答 (1 件)
Raghavendra Ragipani
2020 年 2 月 6 日
編集済み: Raghavendra Ragipani
2020 年 2 月 6 日
Hi,
While I haven't got a chance to run your code, I have couple of suggestions for you based on the error message.
Please try to correct the bounds for X(2) as tangent function is infinite at bounds. Since, you have initialized it to 15, which is out of specified bounds, matlab might have initialised it to one of the specified bounds and hence the error.
try -pi/2+eps and pi/2-eps as bounds.
and try to initialize within the bounds.
cheers,
raghav
3 件のコメント
Raghavendra Ragipani
2020 年 2 月 7 日
Another suggestion - please check if variables a0, av etc. defined in J are available in the workspace before J is executed. Variables assigned after defining the anonymous function (J) will not be considered.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!