How to connect the simulink to fmincon?

L
Lucas · Sep 22, 2020 · 1.9K views
Question
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?
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Nov 20, 2025

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)

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!