Passing information between level-2 matlab s-functions

17 views (last 30 days)
I am trying to model a two step chemical process using simulink and s function for each step. 5 outputs from step 1 are connected to the 5 inputs of step 2. I initialize the outputs of step 1 in its s function using read in parameters. When I initialize the state variables of step 2, which uses outputs from step 1, the values of the output the output variables of step 1 inside step 2 initialization are zero. What can I do to fix this?
Thanks. Girish

Accepted Answer

Jarrod Rivituso
Jarrod Rivituso on 12 Apr 2011
Some background info that you may find useful...
- Simulink goes through an "initialization" phase prior to starting its simulation loop. Here it will call certain S-function methods for each block, such as the method that will initialize the state variables (for MATLAB S-functions, I believe this is the InitializeConditions block method).
- During the simulation loop, Simulink typically first computes all block outputs, then it will go back and compute updates to state values. This is the typical "Output" and "Update" functions.
One key thing to note is that the initialization part comes prior to any block outputs being produced. On that note, it sounds based on your description that you are trying to use a block input value during the initialization phase of the simulation. This is not going to work for you.
Each block must set its own initial state values based on initial conditions. You could use some MATLAB code outside of the S-functions to pass the correct initial values to each, if that is necessary.
  1 Comment
Jarrod Rivituso
Jarrod Rivituso on 12 Apr 2011
Note that you can use the block sorted order to determine which block executes first. To do this, select Format -> Block Displays -> Sorted Order. The sorted order is determined automatically by Simulink based on which blocks rely upon which other blocks.
To be more clear, my suggestion is that you do the following
1. Write a MATLAB script that calculates the initial conditions for each block in the base workspace
2. Use the initial conditions calculated in the base workspace as parameter arguments to their appropriate S-functions.
3. In each S-function's "Initialize" method, set the initial values of the internal states to the parameter values of the S-function.
4. Use the state values in the "Outputs" method as you require.
Does that make sense?

Sign in to comment.

More Answers (1)

Girish
Girish on 12 Apr 2011
In the 'Start' method of step 1's s function I added a for loop to set block.OutputPort(i).Data for all ports. That made the values visible to step 2's s function. But as it turns out, simulink treats my step 1 block, which only has output ports, as a source block and does not go into the 'Derivatives' function. Since I introduce a step change in one of the outputs in the 'Derivatives' function at a predetermined time, that never happens, and the simulation ends at the specified end time with no dynamics. How do I force simulink to integrate the step 1 block? I know this may sound artificial, since I can do this through the standard simulink blocks. But this is just a test case. What I want to do ultimately is to read in the various parameter values through a text file which is in turn generated from a GUI which makes it easier to play with the model. Thanks for your help. Girish
  4 Comments
Jarrod Rivituso
Jarrod Rivituso on 13 Apr 2011
Generally, continuous state blocks will have the following kind of flow during the simulation loop:
Outputs – creates the block output using the current value of the continuous state
Derivatives – creates the derivative of the block using the current value of the input signal, and possibly other factors as well
Girish
Girish on 14 Apr 2011
After several trial and error attempts, here are the main roadblocks I have hit. This is with reference to my simple example.
1. I accept the Matlab requirement that the only time I can set the value of a state variable is in the InitialCondition function. My biggest hurdle right now is how to get relevant information in the InitialCondition function. For example, I cannot do something like
block.ContStates.Data(1) = block.InputPort(1).Data;
because the input(1) of unit2, connected to output(1) of unit1, does not have the right information. In general, how do I reliably query information associated with any interacting block(s).
2. In the Derivates function of an s function any assignments to block.ContStates.Data are immaterial and have no effect on the outcome.
In light of these Matlab quirks, how can I arbitrarily change the value of a state variable. For example, how do I programmatically implement a step function since the only possibility to set a state variable to a new value is within the Derivatives function. Or can I set it in the Outputs function?
How do I force the integrator to integrate only upto a specific time, and not cross that time. This will allow me to reliably implement if-then logic based on current time which will be guaranteed to be within bounds of a specific time.

Sign in to comment.

Categories

Find more on Programmatic Model Editing 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!