Info

This question is closed. Reopen it to edit or answer.

How to control simulink with fmincon using 2 (two) decision variables

1 view (last 30 days)
I have a simulink file that I want to control. I want to minimize my objective:
function q = obj(cr,tr) % cr, tr are the variables that I want to be manipulated
sopt = simset('solver','ode3','SrcWorkspace','base','DstWorkspace','base','SaveFormat','Array');
[~,~,yo]= sim('sgs2.slx',[0 10], sopt);
q=abs(yo(end,1)-yo(end,2));
end
I call fmincon
x=fmincon(@obj,xo,[],[],[],[],xmin,xmax,[],options);
the optimization runs but fails.
I think that the way that passing the decision variables is wrong but I have no idea what to try.

Answers (1)

Titus Edelhofer
Titus Edelhofer on 13 Nov 2014
Hi,
do you have other variables in your model apart from cr and tr? If not, you should select 'current' for SrcWorkspace and DstWorkspace. This way Simulink would grab the variables cr and tr from the input variables.
Second: your objective function needs to be declared differently. It must have the form obj(x) where x is a vector (of size two in your case). So you would need to change
function q = obj(x)
cr = x(1);
tr = x(2);
Titus
  4 Comments
Titus Edelhofer
Titus Edelhofer on 13 Nov 2014
Yes, I do: the values are not exactly the same, but very similar. For the jacobian it computes the finite differences
df/dcr ~ (f(cr+h, tr)-f(cr,tr))/h
and
df/dtr ~ (f(cr, tr+h)-f(cr,tr))/h.
So you see three (similar but not equal) calls to f for each iteration. More generally, for n variables you need n+1 function evaluations per iteration (neglecting the fact that the optimizer sometimes updates the jacobian without recomputing it).
Titus

Community Treasure Hunt

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

Start Hunting!