How do I tune a parameter in an accelerated model reference while the simulation is running?

I have a parameter in a model reference that I would like to tune while the simulation is running in accelerator mode. How do I do this?

 Accepted Answer

There are many ways to tune a parameter in accelerated model reference while the simulation is running. Consider the following model and model reference.
Tune with Simulation Object (24a and later)
The easiest way to tune a parameter while simulating is to use the Simulation Object introduced in 24a.
set_param(mdl,'SimulationMode','acc') sl = simulation('model'); gainRM = 1; sl.start pause(1) sl.setVariable('gainRM',2) pause(1) sl.stop; sl.SimulationOutput.yout{1}.Values.Data(end) %should be 2
Tune in Normal Mode (No Update)
In normal mode you can tune the variable directly if you use set_param and it will work.
set_param(mdl,'SimulationMode','normal') gainRM = 1; set_param(mdl,'SimulationCommand','start'); pause(1) gainRM = 2; set_param('Ref_Mod/Constant','Value','gainRM'); pause(1); set_param(mdl,'SimulationCommand','stop'); out.yout{1}.Values.Data(end) %should be 2
Tune in Accel Mode (With Update)
In accelerator mode, you need to trigger an update for the change to propagate.
set_param(mdl,'SimulationMode','acc') gainRM = 1; set_param(mdl,'SimulationCommand','start'); pause(1) gainRM = 2; set_param(mdl,'SimulationCommand','update'); pause(1); set_param(mdl,'SimulationCommand','stop'); out.yout{1}.Values.Data(end) %should be 2
Tune in Accel Mode (With Model Arguments)
If you do not want to trigger an update, make the parameter a model then you do not need to trigger an update.
set_param(mdl,'SimulationMode','acc') gainRM = 1; set_param(mdl,'SimulationCommand','start'); pause(1) gainRM = 2; instSpecParams = get_param('model/Model1','InstanceParameters'); set_param('model/Model1','InstanceParameters',instSpecParams); pause(1); set_param(mdl,'SimulationCommand','stop'); out.yout{2}.Values.Data(end) %should be 2

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Release

R2020b

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!