Optimisation using a simulink model called from inside a MATLAB function

11 vues (au cours des 30 derniers jours)
Hamidreza Zandi
Hamidreza Zandi le 24 Oct 2020
Hi everyone !
I'm trying to solve an optimisation problem using a simulink model (with simscape Specialized Power Systems devices)
My objective function is defined by a MATLAB function where I call the simulink model using 'sim' command. I both used 'fmincon' (with SQP) and 'ga' however things are being a bit weird :
When using 'fmincon' the optimisation stucks at the initial point while I can see in simulink that the objective function value is being changed and the initial point was defenitely not a local minimum. After a few seconds 'fmincon' stops and tells me the objective function in non-decreasing.
Using 'ga' I can also see the objective function being optimised in simulink using a 'Display' but I have a blank MATLAB command line.
I tested both my 'fmincon' and 'ga' code using an analytical function instead of simulink model and they just word fine with iterations displayed in command window during the optimisation.
Thank you all in advance for you comments or solutions.
++ I attached a very simple simulink model which aims at maximazing the current flowing in a resonant circuit by changing the values of a capacitor and inductance with the example of the code I used.
The objective function is defined in "Irms_test.m", the file calling optimisation commands is "Mait_opti_test.m", and before running the code the simulink file should be manally openned as it is not loaded in the function.

Réponses (1)

Joel Van Sickel
Joel Van Sickel le 17 Nov 2020
Hello Hamidreza,
one of the issues is that you need to make sure that the initial iterations perturb the system enough that the value is different. In this case, it is essentially simulating three different scenarios, and getting the same IRMS for all of them, so it has no idea what direction to search. You can use this function to increase the size of the step taken so that it will see differences in the simulation.
options=optimset('Display','iter','Algorithm','sqp','TolX',1e-15,'MaxFunEvals',1000,'MaxIter',1000,'FinDiffRelStep',1);
this is not necessarily the ideal value, but I was able to run the optimization. You may want to try with a simpler optimization set where you aren't specifying algorithm and tolX until you get the rest working well.
Also, I had to rewrite your funciton to load and close the simulink model based on the changes it was making (without saving them).
C1=x(1)*1e-3;
L1=x(2)*1e-2;
load_system('test_rlc');
set_param('test_rlc/C1','Capacitance',string(C1));
set_param('test_rlc/L1','Inductance',string(L1));
options = simset('SrcWorkspace','current');
res=sim('test_rlc',[],options);
close_system('test_rlc',0)
Irms=res.simout(end)
r=1/Irms;
Regards,
Joel

Catégories

En savoir plus sur Trimming and Linearization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by