How can I change the parameters of a Simulink model during simulation?

I have a Simulink model. Now, I'd like to quickly test a set of parameter values used in the model and observe the effects. How can I change the parameters of a Simulink model during simulation?

 Accepted Answer

There are many ways to achieve it, depending on your use case:

1. Saving and restoring the simulation state as the 'SimState':

The Simulink 'SimState' feature allows you to save all run-time data necessary for restoring the simulation state of a model. A SimState includes both the logged and internal state of every block (e.g., continuous states, discrete states, work vectors, zero-crossing states) and the internal state of the Simulink engine (e.g., the data of the ODE solver). Note: Since R2019, 'Save complete SimState in final state parameter' was renamed to 'Save final operating point'.
Please refer to the following documentation link to learn more about the functionality:

2. Tunable Block Parameters:

Simulink lets you change the values of many block parameters during simulation. Such parameters are called tunable parameters. You can tell whether a particular parameter is tunable by examining its edit control in the block's dialog box or Model Explorer during simulation. You can also tune a parameter at the MATLAB command line, using either the "set_param" command or by assigning a new value to the MATLAB workspace variable that specifies the parameter's value. Note: You must update the model diagram after you change the value of a workspace variable; using "set_param" does not require this.
Refer to the following documentation link to learn more about using tunable parameters:

3. Simulink.Parameter:

This class enables you to create workspace objects that you can then use as the values of block parameters (for example, the value of the "Gain" parameter of a Gain block). You can create a Simulink.Parameter object in the base workspace, a model workspace, or a Simulink data dictionary. Note: You also have to update the model diagram after you change the value of a Simulink.Parameter.
Refer to the following documentation link to learn more about 'Simulink.Parameter':
If a block parameter needs to be evaluated (and changed if needed) at every simulation time step, a programmatic way of stepping through the simulation is to use the following command in a loop:
>> set_param(modelName, 'SimulationCommand', 'step');
Users can also employ event listeners to make parameter modifications only when certain events happened. Below is an example:

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products

Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!