How to connect the simulink to fmincon?

Illustration
Lucas - 2020-09-22T10:30:04+00:00
Question: How to connect the simulink to fmincon?

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 answered . 2025-11-20

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)


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!