Please help!!!!Error: Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
이전 댓글 표시
I am trying to run this code but Im getting a bunch of errors.
t=0:.1:5;
u(1:25)=.1;
u(26:51)=-.1;
plot(t,u);
[to,yo]=sim('hw',5,[],[t',u']);
%Finding u_final and cost
l_b=ones(51,1)*(-50);
u_b=ones(51,1)*50;
options=optimset('Display','iter','PlotFcns','optimplotx');
[u_final,cost]=fmincon('find_cost',u,[],[],[],[],l_b,u_b,'find_constraint',options);
[t_f,x_f,y_f]=sim('hw',5,[],[t' u_final']);
These are the functions used
%Cost function
function cost=find_cost(u)
cost=(1/2)*.1*trapz(u.*u);
end
%Constraint function with constraint x(5)=15
function [cineq,ceq]=find_constraint(u)
cineq=[];
t=0:.1:5;
[tout,yout]=sim('hw',t',[],[t',u']);
ceq(1)=15-yout(end,1);
ceq(2)=yout(end,2);
ceq(3)=yout(end,3);
ceq(4)=yout(end,4);
ceq(5)=yout(end,5);
end
Kindly help me I need to do this..
답변 (1개)
Walter Roberson
2018년 2월 23일
Your code assumes that find_constraint is being passed a row vector, u, but fmincon makes not such guarantee. The documentation says that the nonlinear constraint function must accept a vector or array, but there is no documentation about when it will be any particular shape.
I recommend that you use
[tout,yout]=sim('hw',t',[],[t',u(:)]);
댓글 수: 6
Aishwarya Bangalore Kumar
2018년 2월 23일
Walter Roberson
2018년 2월 24일
I would need your simulink model to test further.
Aishwarya Bangalore Kumar
2018년 2월 24일
Walter Roberson
2018년 2월 24일
편집: Walter Roberson
2018년 2월 24일
>> sim('simfig')
No system or file called 'simfig' found.
>> sim('simfig.PNG')
The file name 'simfig.PNG' is invalid because it does not have the extension ".mdl" or ".slx".
>> !cp simfig.PNG simfig.mdl
>> sim('simfig')
Warning: simfig.mdl, line 1: Unexpected characters at or before line 1, column 8
File '/Users/roberson/MATLAB/3/384/384494/simfig.mdl' does not contain a valid Simulink model
>> !cp simfig.PNG simfig.slx
>> sim('simfig')
File '/Users/roberson/MATLAB/3/384/384494/simfig.slx' does not contain a valid Simulink model in SLX format: Could not open source package
Your model seems to have been corrupted when you attached it.
Aishwarya Bangalore Kumar
2018년 2월 24일
Walter Roberson
2018년 2월 24일
You can zip the model and attach the zip
카테고리
도움말 센터 및 File Exchange에서 Get Started with Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!