How to connect the simulink to fmincon? Thanks

1 view (last 30 days)
Hello, everyone. I am a matlab learner. I have some proble associated with the combination with the simulink and fmincon. Thank you for any suggestion and helps.
Here is the code:
function main
close all; clc;
x0=[1.6 100];
Lbnd=[0.5 90] ;
Ubnd=[2.5 450] ;
[x,y,exitflag,output]=fmincon(@objtime,x0,[],[],[],[],Lbnd,Ubnd,@Constt)
end
function ftime=objtime(x)
Kpi=x
options = simset('SrcWorkspace','current');
[t,xstatus,y]=sim('control_a2')
ftime=y(:,3)
end
function [C,Ceq]=Constt(x)
C(1)=x(1)-2.5
C(2)=0.5-x(1)
C(3)=x(2)-450
C(4)=50-x(2)
Ceq=[]
end
The simulink is called in object function. But when I run the code. The "x" design variables are not changed. The optimization is not implemented. I am not sure whether is related with the workspace problem or with other aspects? Could you give me some idea and solution?
Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Aug 2011
In your function "main", you assign to x, y, and so on, but those are local variables, so their values are thrown away after "main" runs. You may need to use something such as assignin('base','x',x)
  2 Comments
J T
J T on 8 Aug 2011
Hi Walter, 100% correct.
But I am just crious about the reason. It seems that I can ignore the "assignin" if no simulink model is called for main function.
Thank you
Walter Roberson
Walter Roberson on 8 Aug 2011
Sorry I do not know how Simulink really interfaces.

Sign in to comment.

More Answers (0)

Categories

Find more on Manual Performance Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!