Programming simulink model to pause and fetch infrmation at a specific time

1 view (last 30 days)
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
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?
Rhonda Jordan
Rhonda Jordan on 10 Feb 2011
Hi Douglas,
my function currently is expecting 3 10-element vectors for past values of a,b, and c.
'e' will be used as a constant value (or a time-varying signal) and multiplied to other variables within the simulation model.

Sign in to comment.

Answers (2)

Guy Rouleau
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

Doug Eastman
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
Rhonda Jordan
Rhonda Jordan on 11 Feb 2011
Hi Douglas and Guy,
I have two follow-up questions:
(1) my model is continuous time - I can't seem to use the Zero-Order Hold or even the Unit Delay block after the Tapped Delay block without getting the following error:
Illegal usage of SS_OPTION_RATE_TRANSITION by S-function 'sfix_udelay' in 'wom_test/Tapped Delay'. This option can only be used by S-functions with a discrete sample time.
Is there a block that I can use with continuous time signals that allows me to sample a signal at every major time step (in this case a sample time of 1 where the sample time of the overall model is 0.1) and hold the value until the next time step? I attempted to use the Sample and Hold block with the Trigger Block but it did not allow me to set the sample time - it had to be inherited.
(2) The function that I'm attempting to embed within the simulink model needs to accept vectors a,b and c but should only be executed every major time step (sampling time of one) but I need it to produce a continuous time signal that only changes value every major time step (it may or may not change value). I'm not quite sure how to formulate this S-block and don't know where to start. Can you help me think through this?
Rhonda Jordan
Rhonda Jordan on 15 Feb 2011
Hi Douglas and Guy,
Thank you for all of your help!
(1) I was actually able to use a tapped delay without a zero-order hold block or a sample and hold block; the tapped delay alone produced what I needed.
(2) I created an Embedded MATLAB block to take care of the tasks I outlined in my original question from 8 days ago; in order to get the signal to only update at major time steps, I sent the clock into the Embedded MATLAB function block and only executed certain commands when the clock was at major time step values; I also declared my signal to be a persistent variable so that it could be updated and held in memory every time the function is called; at minor time steps, I just used the value of the previous time step (this process mimics the sample and hold property that I wanted).
Thank you again!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!