S-Function Level 1 vs S-Function Level 2

44 views (last 30 days)
Miguel Montilla
Miguel Montilla on 22 Mar 2012
I have a problem. I have modeled a dynamic system( 5 inputs and 10 outputs) using S-Function Level-1 and Level-2. Both models work very well. My question is: Why the model that uses S-Function Level-2 runs its simulation very slowly? With the level 1 model simulation is very fast.
  1 Comment
want2know
want2know on 15 Jul 2013
Dear Miguel,
I have read your post and I understand that you have successfully developed your system in S-function level 1.
Do you mind to spend a couples of minutes to read the following: please
I have been using S-function to do the following:
[a1, b1] = choose_cells(c, d);
where a1 and b1 are outputs, c and d are inputs. All the variables are having a single value, except d is an array with 6 values.
Referring to the image attached, we all know that in S-function block, the input dimension must be SAME as output dimension, else we will get error, in this case, the input dimension is 7 while the output dimension is 2, so I have to include the "Terminator" blocks in the diagram for it to work perfectly, otherwise, I will get an error.
My problem is, when the system gets bigger, the array d could contain hundreds of variables, using this method, it means I would have to add hundreds of "Terminator" blocks in order to get this work, this definitely does not sound practical.
Could you please suggest me a wise way to implement this?
Thanks in advance.

Sign in to comment.

Answers (4)

Miguel Montilla
Miguel Montilla on 22 Mar 2012
How can I calculate CPU time when running the model in simulink???

Kaustubha Govind
Kaustubha Govind on 22 Mar 2012
I don't have a definite answer, and it's difficult to say not having seen your code, but it could be because Level-2 S-functions have a lot more features than Level-1 S-functions, so there is probably a lot of heavy-lifting/back-end work done by them? Exactly how slow are we talking about? Can you provide a ratio?
  1 Comment
Miguel Montilla
Miguel Montilla on 22 Mar 2012
The level 1 model has an elapsed time equal to 7.139867 seconds.
The level 2 model has an elapsed time equal to 237.99 seconds
Both models have the same numbers of state variables (6) and 5 inputs and 10 outputs.

Sign in to comment.


Ben
Ben on 23 Mar 2012
Profile your code but ticking the 'profile' entry in the Simulink tools menu.
You will probably see that the Level-2 function has its state functions called several thousand times more often than the Level-1 one does (for example my Level-1 function has its output function called ~6000 times, the Level-2 output function is called ~450,000 times!). This is the source of the slow-down, but I don't know how to fix it, sorry. Trying to work that out myself now!

Ben
Ben on 23 Mar 2012
Solved it for my particular simulation: I had direct feedthrough incorrectly enabled for an input port. This created an agebraic loop.

Community Treasure Hunt

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

Start Hunting!