I need to integrate the simulation of a circuit in a MATLAB optimization algorithm. The optimization operates on the value of an inductor, changing it before running the simulation. However, this value is also used inside the initialization script to compute several other components values.
If I set it via ModelVars the initialization script fails. I understand this is because ModelVars are evaluated AFTER the initialization scripts.
Is there any other way to set such variable BEFORE the initialization script?
Thank you!!
Is there any other way to set such variable BEFORE the initialization script?
The execution order is always the same, as explained in this graphic from the XML-RPC Interface and Controller Design in Python tutorial
.
You want to move the parameter dependent code in the Model Initialization Commands to execute at a later stage. There are a few options:
- Move the code into your MATLAB script and pass all variables via the XML-RPC interface by assigning all dependent values in the ModelVars structure. Note you can directly pass a struct from MATLAB to PLECS which might be more convenient way to pass a large number of variables. You can also define an external *.m file with a function that assigns the relevant values. That way the function can be called from MATLAB or Model Initialization Commands.
- Move the code into a Masked Subsystem. Then you can pass in a reduced set of parameters into the masked subsystem, say your inductance and saturation point, and the dependent variables can be calculated in the Mask Initialization commands.
Thank you very much Bryan! As you suggested, I managed to move everything in MATLAB to send the update parameters altogether. Nice idea using the masked subsystem however, I’ll take it into consideration for the next occasions.
Thanks again!