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

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

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

댓글을 달려면 로그인하십시오.

답변 (2개)

Guy Rouleau
Guy Rouleau 2011년 2월 7일

1 개 추천

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 2011년 2월 10일

1 개 추천

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

Douglas,
Thank you again for your help with this. One quick question - what do you mean by zero-order hold block? I've looked for the block in the library and can't seem to find it.
Actually I found the block. Nevermind and Thank you so much!
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?
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!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품

질문:

2011년 2월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by