Weird issue from a simulink model (a simple sfunction block in series to a switch) Could it be a bug on Matlab 7.8 (2009a) ?

1 view (last 30 days)
Hello everyone,
I created a model with simulink composed by a c-mex sfunction block and an upstream switch connected to the sfunction input. The C sfunction has two input: IV IN, one output, Y and one discrete state variable. This switch is supposed to give the initial value of the state variable x of the sfunction block during the execution of the mdlInitialize. The output of the sfuction, y, is simply the state variable x. The update of the sfunction is just x=IN
So here it is a sketch of the code:
Init
x=IV
Output
y=x
Update
x=IN
As inputs of the switch I put three constant c1, c2 and c3. The condition on the switch is to pass the first input if c2>0.
I chose this value
c1=1
c2=1
c3=2
in this way the output of the switch is 1, so IV equals to 1 And I chose IT constantly equals to 1
After several simulations, performed on both a variable step time solver and a fixed step one (runge-kutta), the output of the sfunction has a stunning behavior at the beginning, it's equal to zero, instead to be equal to 1
even if all the "scope"/"display" blocks and all the debugging tests performed (by Visual C) shows the IV variable is equal to 1 from the beginning.
Even if I replace the switch with a gain block, I have the same behavior. The only way to have the logical and excepted behavior is to directly connect a constant block to the sfunction
is that a bug in Matlab?
Actually I sorted out by making a switch using a sfunction with an init stage, but this outcome is still bizarre and puzzling
I use Matlab 7.8 (2009a)
thanks 4 reading

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 13 Jun 2012
What you are seeing is expected behavior. As you know, all Simulink blocks (like S-functions) have several methods, which are run in a certain order during the course of a model's simulation. The Process View flowchart gives you a detailed view of what order the methods are called in. The issue here is that the Output method of the Switch block (which computes the signal "IV") is only run after Simulink finishes running the Initialize method on all blocks. So at the time that the S-function is initialized, the output of the switch has not been computed yet. So at initialization time, IV=0. Unfortunately, the time by which the value IV=1 is available is too late for initializing the state in your S-function.
What you're trying to do is introduce an "External Initial Condition (IC)" input on your S-function. Unfortunately, I don't think S-functions currently have this ability. The IC can at most be an S-function parameter that you can set before simulation begins.
I understand that your S-function may in reality be more complicated than a Delay, but I thought I should bring to your attention that the Unit Delay block in Simulink does provide an option for an External IC. Perhaps you can use this block in combination with your custom logic to achieve what you need?
I would also recommend confirming this limitation (of not being able to create an S-function with External IC) with MathWorks Tech Support and put in an enhancement request so that the development team of this need and can prioritize it if there is enough demand.

More Answers (0)

Categories

Find more on Simulink 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!