By Guy Rouleau
One question I often receive is:
How can I change the value of a block parameter while the simulation is running?
A good example of this type of question is the following: How do I model a transfer function with coefficients that vary with simulation time in Simulink?
Every time I receive this question, I end up explaining the difference between block parameters and signals in Simulink. Here is how I explain it:
In many scripted languages like MATLAB, data is stored in variables. Whether the data changes frequently or not, it is still data stored in a variable and the programmer can change it whenever he or she wants.
In Simulink, this is different. Simulink is a tool designed to simulate the response of dynamic systems over time. To do this efficiently, data is divided into 2 categories: Signals and Block Parameters.
In Simulink, a signal is a piece of data intended to change frequently, possibly every time step. You can find a detailed definition of signals in the Simulink documentation:
A block parameter is an attribute of a block. Some block parameters, especially mathematical parameters, can be defined as tunable parameters. It is possible to change the value of a tunable parameter while the simulation is running from the block's dialog parameter, the model explorer, or using the set_param command.
Why not changing block parameters every time step?
Based on the information above, many people try to implement a MATLAB-file S-function or an Event Listener that call set_param to change the tunable parameter of another block at every time step. This can work, however, there are some major drawbacks. (Including the fact that Simulink is not intended to be used this way!) Each time you call set_param on a model, Simulink re-evaluates block parameters, and depending on how many get evaluated, this can be slow. Using MATLAB calls to set_param also preclude the model from ever being compiled into a real-time application.
In my opinion, the biggest problem is that when you use set_param to change a block parameter based on the value of a signal, you hide data dependencies from Simulink.
During model compilation, Simulink determines the sorted order of blocks. This sorted order is based on the data dependencies defined by signals. To ensure that your model behaves as you expect, you must tell Simulink what dependencies exist between your blocks. (This is done by passing the data through signals!)
What can I do if I really need to change the parameter of a block every time step?
or if you need a sine wave with tunable amplitude, frequency and phase, build it with basic blocks:
I agree that those examples are relatively simple. But even for more complex examples it is usually not too difficult to make your custom subsystem.
Finally, if there is a block parameter in the Simulink library that your application would require to be a signal, I recommend contacting technical support about it. Our engineers will be able to guide you in the right direction and submit an enhancement request to our development team.
An example of a block whose parameter can also be an input signal is the Direct Lookup Table (n-D). At every time step, you can change the table data if you enable the 'Make table an Input' option.
Now it's your turn
What block parameters do you want as a signal? Leave a comment here with your suggestions
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.