Programming simulink model to pause and fetch infrmation at a specific time
2 views (last 30 days)
Show older comments
I have the following challenge:
- I am running a simulink model for 15 years (year-long time steps) with variables a, b, and c that change over time
- my sampling time is 0.1 of a year
- at the start of every year (for example, at time t = 2), I need to modify a constant value e in my simulink model; BUT at time t=2, e = f(a,b,c) for t = 1 to 1.9 (ie the value of e at t=2 is a function of the evolution of a, b, and c when t was 1 to 1.9)
- the function used to determine the constant value e at the start of each year is an m-file
I am new to simulink; I know how to pass data to and from the matlab workspace and simulink model (for example, using the From Workspace block) but this assumes I know what the value of my constant/parameter will be at each time step. Here, the value can only be determined using the evolution of variables internal to the simulink model and I cannot know them in advanced.
Therefore, my logic is that I will have to pause the simulation at time t = 1.9 for example, pass the data (a,b,c) to the m-file to calculate e and then return the new value e so that the simulation can continue.But I need to do this at each time step (at t = 1, 2, 3, ... 15).
Can anyone help here?
Thank you so much!
2 Comments
Doug Eastman
on 7 Feb 2011
What is the input to your existing MATLAB Function? Is it expecting 3 10-element vectors for past values of 'a' 'b' and 'c'?
How do you use 'e' in the model? Is it just used in a Constant block or is it a parameter in other types of blocks? If so, what kind of blocks?
Answers (2)
Guy Rouleau
on 7 Feb 2011
Typically in Simulink you want to use signals for time varying data and parameters for non-varying data.
Based on your description, "e" should be a signal. If you prefer implementing the equations to compute it in MATLAB code, you should use a Level-2 MATLAB s-function, or the Embedded MATLAB Function block. See this for more details on implementing MATLAB code in Simulink:
http://blogs.mathworks.com/seth/2010/07/18/including-matlab-code-in-a-simulation/
Then you can set the implementation you choose to run at a sample time of 1 second. Read this section of the doc to learn how to deal with sample time:
http://www.mathworks.com/help/toolbox/simulink/ug/br09i6c.html
I hope this helps
0 Comments
Doug Eastman
on 10 Feb 2011
Just to add a little more onto what Guy said, for your case it sounds like you'll need to "buffer" the a b and c values to send vectors into the function 'f'.
For this you can use a Tapped Delay block with 10 delays and a Sample time of 0.1 followed by a Zero-Order Hold block with a Sample time of 1 for each signal a, b, and c. These three signals can be sent into an Embedded MATLAB Function block with the definition for f as you currently use it.
The output of that block can be used throughout your model as the value e as Guy mentions above.
4 Comments
See Also
Categories
Find more on Sources in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!