Guy on Simulink

Simulink & Model-Based Design

How Do I Change a Block Parameter Based on the Output of Another Block? 34

Posted by Guy Rouleau,

Blogger, Guy RouleauBy 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:

Data

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 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.

Signals

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:

Block Parameters

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?

The approach I recommend is to implement your algorithm using signals and basic blocks. For example, instead of using a Gain block, use a Product block:

Converting from parameter to signal

or if you need a sine wave with tunable amplitude, frequency and phase, build it with basic blocks:

Sine Wave with variable amplitude, frequency and phase

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

34 CommentsOldest to Newest

wei replied on : 1 of 34

@Guy, There is probably furhter limitation with set_param/event_listener if model is intended for code generation. I have to say that the signal-parameter separation is not as natural to software engineer as to control engineer.

Bob replied on : 2 of 34

@wei Software engineers that treating parameters different than signals is something we’ve struggled with. They are just so used to swapping a pointer to get a different parameter set.

Unfortunately, we have not gone down the suggested route. We had custom storage classes that let users select among parameter sets before initialization. Then the users wanted to select at runtime, and that was easy to do by just relaxing the restrictions on when the pointers could swap. Then the users wanted simulation behavior to match, and that was possible using the set_param/event trigger/model update mentioned.

We did investigate using signals instead of parameters with changing values. The problem wasn’t with blocks that didn’t accept signals, but with finding efficient parameter value selection logic. The custom storage class allows only the pointer to remain in RAM, and combinations of switch and bus selector blocks just seemed to want to copy lots of data to RAM, sometimes multiple times if the winnowing occurred in several stages.

Paul J. replied on : 3 of 34

Could someone describe a typical use-case or two that demonstrates the utility of tuning a block parameter during simulation?

EBS replied on : 4 of 34

@Paul, I seem to remember doing this for some built-in blocks that don’t yet have the option to add an optional extra input to affect some internal operation of the block, like some of the more sophisticated blocks do.

Guy Rouleau replied on : 5 of 34

@Wei, you are right. set_param and event listener are not compatible with code generation.

@Bob, thank you for sharing your experience. It is interesting to see how you used set_param and event listeners to mimic the behavior of a customization you made to the generated code.

KMR replied on : 6 of 34

@Paul, the canonical need to tune block parameters during simulation (or running of actual code) is to schedule things like transfer function coefficients in your controller (or plant model) as a function of things in the environment. For example, assume you’re doing aircraft flight control: the plant (the aircraft) can vary enormously over its flight envelope, so you often have to design controllers that vary their characteristics – say a structural notch filter that has to vary with aircraft weight. So you need to have the filter coefficients vary with aircraft weight. Currently the only way to do this sort of thing in a way supported by code generation is to do what Guy suggests and build these filters out of basic blocks.

Interestingly enough, of Matlab/Simulink’s two competitors MATRIXx/SystemBuild and EASY5, only EASY5 supported this capability, and it had no support for code generation, at least in the timeframe (mid-80’s to early-90’s) that I used it. I did the same workarounds in MATRIXx/SystemBuild that I now do in Matlab/Simulink; the problem has not gone away.

I wish that the Mathworks would make, for example, transfer function coefficients be optional inputs. But there are other fundamental problems with the Simulink TF blocks, most notably that they don’t support setting the initial output, that causes us to have to roll our own regardless…

Paul J. replied on : 7 of 34

@KMR, thanks for the feedback. However, I don’t think this example justifies the need for Simulink to support block parameter tuning. When I’ve needed to schedule controllers in Simulink, I did exactly as you described, i.e., wrote my own subsystem that takes the control gains as inputs after they’ve been computed elsewhere. I don’t think of this as parameter tuning; I think of this as how the controller design is implemented. To me, parameter tuning means changing the value of the parameter outside of the simulation equations themselves, which I have never wanted to do.

OTOH, my concern is that there are some (a lot of?) costs for Simulink to implement block parameter tuning. But I have yet to see an example where the ability to tune block parameters during a run is the only solution, or even a solution that far outweighs alternatives in terms of implementation complexity, that justifies these costs. For example, I’ve always wondered how much time is spent during a simulation checking for changes in block parameters. Suppose one has a huge matrix as a parameter in a table look-up block. How much time does Simulink spend determining whether or not the table has changed during a run? I’ve had some experience that suggests it’s not trivial; see #5 here http://blogs.mathworks.com/seth/2010/12/06/5-resources-for-simulation-performance/ (can’t know for sure because I don’t know exactly how Simulink works).

BTW, have you checked out any of the gain scheduling blocks in the Aerospace Blockset? I haven’t tried them myself…

KMR replied on : 8 of 34

@Paul J., the reason that I want the Mathworks to implement support for, e.g., driving transfer function numerator and denominator coefficients from inputs external to the TF block is specifically to be able to STOP rolling my own implementations of these blocks. That’s supposed to be one of the advantages of using Simulink/RTW! But for real systems, we do a lot of wheel-reinventing because of the lack of support of this feature.

I don’t use Aerospace Blockset, as I don’t see the value added for my organization – that’s core competency stuff for us.

Paul J. replied on : 9 of 34

@KMR, I’m not disagreeing with the desire/need to have a transfer function block with external num/den coefficients. I just don’t see the lack of that functionality as a good example of the need for block parameter tuning. If that block were provided, it wouldn’t really be parameter tuning at all, it would just be another block. But given we don’t have that block now, I’m interested in your thoughts on how best to implement that functionality. Would you roll your own block with those num/den coeff inputs? Or would you try to do something outside of the block diagram signal flow to tune the coefficients of the current TF block during a run? If the former, then it sounds like what’s really needed is a new block, not the ability to tune block parameters during a run. If the latter, why is that the preferred approach?

gulliber replied on : 10 of 34

Slider gain’s block is the solution. It allows you to change in real time one variable. Then you can associate this variable to parameter you want to vary.

elm replied on : 11 of 34

i don’t understand the role of the look up table and every parameter in its block parameters

IM replied on : 12 of 34

The implementation of time-variant TF block using integrators and other basic blocks seems reasonable. However, if I feed the model with signals representing TF characteristics, for example, the damping ratio, the model seems to be excitated by changes in these signals, not only by the actual input signal.

Question: How to prevent this from happening? If I implement the time-variant model with ‘set_param’ called every time step, there’s no problem (except for extremely slow simulation).

Simone Leon replied on : 13 of 34

At present I am working on a Simulation Model containing 2 subsystems in Matlab Simulink:
•Subsystem A – generates some data
•Subsystem B – reads the generated data from the workspace

Right now each subsystem is in a different Simulink file. I would run Subsystem A first. This data would be saved to the workspace and then I would run Subsystem B which reads the data from the workspace.

I would like to put these two models together in one simulation window. However the problem is that each model runs at a different simulation time. And in order to read the data form the workspace the first simulation must first stop and the variables sent to the workspace.

Is there any way to stop the simulation for the Subsystem A, therefore sending the variables to the workspace. And then Subsystem B would be executed to read from the workspace.

I tried using the trigger and enable blocks but because the simulation does not stop and send the variables to the workspace before Subsystem B is activated, Subsystem B is unable to read the data generated by Subsystem A. I have done some research and it shows that simulation must stop in order to be able to read from the workspace.

Is it therefore possible to have these two subsystems in one file sharing the same space? I would like Subsystem A simulation to start and stop, therefore sending the variable to the workspace and Subsystem B is activated automatically after the variables were sent to the workspace.

Please remember that:
•Subsystem B needs the variables generated by Subsystem A
•Subsystem B reads from the workspace
•Both subsystems have different simulation stop times
•Subsystem A must stop first in order to send the variables the workspace

I have tried using the Variant Subsystem Block. But I realized that yous still have to automatically change the value of the workspace variable when initializing the model.

Also the sim command is very useful for this project but I would rather everything be synchronized in one model rather than using a sim command

Valentina Hernandez Piedrahita replied on : 14 of 34

Hi.

I’m trying to tuning a transfer fcn’s parameter but I have not figure out how. If you can explain me how to use the set_param command to do it, I’d appreciate that.

I think it woulb be interesting if I can do it converting the transfer fcn into a product, but I don’t know how to do it.

Any help would be ok for me. Thanks

Chandan replied on : 15 of 34

I need to change the tunable parameters in a model at specified time.

I understand that in such case, best way is to rework the model so that parameter’s job is done by additional inport.
However, some other constraints do not allow me to make such sweeping changes.

There is a mention of doing set_param with event handlers. What event handlers we are talking about here? I use R2011B.

PS: The link that has been provided for event handler is not working for me as it refers earlier versions. I have valid license but looks like it is not associated.

RobM replied on : 17 of 34

This is a topic of enormous interest and I hope I can both add and learn something here. I develop real-time simulators, and recently started working on exclusively Simulink-developed simulator architectures.

In this environment, reconfiguration of the simulator requires the reparameterisation of the compiled models running on the real-time target.

Initially, models were parameterised with MATLAB structures that did not enable tuning. Implemented Simulink.Parameter structures in place of the MATLAB structures enables parameter tuning and requires no modifications to block parameter values (references) in the models. The parameter structures are defined by Simulink.Bus objects and are set to exportedGlobal storage class, generating logical C structures in generated code.

There still appears to be some limitations on how to write to parameters in real time in the Simulink environment, however.

The current development is to use a simulink coder output block to write the contents of an input bus structure to the block parameter structure.

I’m looking at the C API too, but both seem cumbersome, and somewhat contrary to the Simulink philosophy.

As previously mentioned in earlier comments, perhaps the issue is the blurring of the distinctions between parameters and signals… it’s the constant vs coefficient discussion all over again.

JWL replied on : 18 of 34

I’ve run into several situations where having tunable parameters is not only advantageous but necessary. For instance, I am using Simulink to program my dSPACE hardware. One of my hardware functions is an encoder reader for a linear encoder. I am using an index pulse to reset the position measurement to avoid drift, however the triggered block I am using to reset position uses a block parameter for the reset value. I need this value to be dynamic, and not only manually tunable (which I know I can achieve through ControlDesk), but programmatically tunable. I can’t find any way around this and it represents a huge roadblock to my project, to the point that I am currently investigating other platform options.

Guy Rouleau replied on : 19 of 34

@JWL, It is usually possible to modify the blocks to take the value as a signal input instead of a parameter. I am not sure exactly which block you are using, but if this is an s-function and you have the source, it should be possible to modify it. If you do not have the source, I would recommend contacting the creator of this s-function.

If you are using only blocks from the Simulink Library, I would recommend contacting technical support with an example. By looking at your specific model, we will be able to provide guidelines on how to modify the implementation and get the behavior you are looking for.

Suraj Prakash replied on : 20 of 34

HI,
I have a doubt.
Can you guide how to control a simple switch (manually) in Simulink during program execution.

That will be a great help for me GUYS.

shady replied on : 21 of 34

i want to make a transfer function of ((1/(s+a)) but ‘a’ is a signal that changes with time (calculated from another equation) but the block in simulink Transfer Fcn uses constant values according to my understanding

Khalid replied on : 22 of 34

Hi guys, I want to add an extra parameters to the model which is already available in the matlab library instead of (P) in DEE function I want to make it as (k^m) since k & m are the new parameters.

Thanks for you attention and time

Khalid replied on : 23 of 34

Hi guys, I want to add an extra parameters to the model which is already available in the matlab library (Cassie and Mayr Arc Models for a Circuit Breaker) instead of (P) in DEE function I want to make it as (k^m) since k & m are theexta new parameters.

Thanks for you attention and time

Tariq Andrea replied on : 25 of 34

What if I want to change the ‘VectorOutputValues’ parameter of the SimEvents Event-Based Sequence block. I need to change this vector may be 10-12 times during a typical simulation?

When I use the following command
set_param (‘jj/ebs’,’VectorOutputValues’, ‘[2 4 3 1]’);

in a Matlab Function, I get the following error message

“Call to MATLAB function aborted: Cannot change the dimensions of run-time parameter ‘VectorOutputValues’ in ‘jj/ebs’ from [1×3] to [1×4] while model is executing”

Tariq Andrea replied on : 26 of 34

I did fix the dimension mismatch issue.

It turns out that the event-based sequence generator does NOT allow parameter change during a simulation.

Here’s my need. I need to generate of vector of input times into SimEvent’s Time-Based Function Call Generator. This vector is a vector of zero’s whose dimensionality changes a few times during the simulation. I have not been able to figure out how to do this.

I would tremendously appreciate any help you could provide.

Thanks.

Wei Li replied on : 27 of 34

Hi Tariq, Currently SimEvents Event-Based Sequence Generator block does not support the changing of sequence values during simulation. As a workaround, can you use the Repeat Sequence Stair block? (in Simulink Source library) To use it, place the block in a Simulink function-call subsystem, create an Outport for the Repeat Sequence Stair’s output, and link the function-call subsystem trigger port to the function-call output of your Time-Based Function Call Generator block. I can send you a screen-shot (or the model) if you need.

Thaibu replied on : 28 of 34

@Wei : I would glad to see the Simulink model with the ‘repeat sequence stair block’ I have a similar issue when I have a vector with change size during the algorithm.

Thank you in advance

Abdul Wali replied on : 29 of 34

Hi Guy,
i have a system which contains two function blocks, each function block contains one parameter in numerator and one parameter in denominator. i want the first block parameters to vary continuously with respect to the second function block so does it mean that i need two input signals and two output signals? or may you suggest my another way on how to define the signals?

Guy Rouleau replied on : 30 of 34

Hi Abdul,

Yes, if one block needs values from another block, those values should be passed as signals.

Abdul Wali replied on : 31 of 34

@Guy
Thank you for your reply, acutally in order to break the function into signals, and then pass those signals to the second block, i still can not manage to code it. so do you have any related example code, so i can get any idea.

Patrick replied on : 32 of 34

@Guy, thanks for the great advice and explanation.
However, I still don’t understand how to update a lookup table from file during simulation.
Say, for example, I have a flight simulator model and want to check altitude against elevation. For this, I have height maps that give elevation for certain area tiles as 2D matrices. As long as I am above tile A, I want to check against height map A. If I cross over to tile B, I want to check against height map B. Since the height maps are quite large, I don’t think it is feasible to have them all in memory initially, so my preferred solution would be to have only the current tile in memory as lookup table, and to exchange the table data with that of another tile if needed. Is there any way this can be done? Or does anyone have another idea how to solve this?
Thanks in advance,
Patrick

Patrick replied on : 33 of 34

@32
Ok, I have managed to get it done using a triggered subsystem that loads my table data.
This can then be fed into a Direct-Lookup-Table (n-D). Still, if anyone has any comments or suggestions regarding this or any other solution to the problem stated above, you are welcome.

@Patrick, To update a lookup table from file during simulation, what I would do is using the Prelookup and the Interpolation blocks. Those can be configured to take the table and breakpoints as inputs. You could then use one From File block per map. Then place each From File block inside a triggered or Fcn-call subsystem and connect all of them to a Merge block, that you then feed to the table input of the Interpolation block.

Add A Comment

What is 5 + 6?

Preview: hide